why is this keep giving me same 2 numbers?

#include <iostream>
#include <iomanip>
#include <ctime> 
#include <cstdlib>
#include <windows.h>
using namespace std;

int generate(int,int);

int _tmain(int argc, _TCHAR* argv[])
{
	int random[1000];
	for(int n=0;n<1000;n++)
	{
		random[n]=generate(1,50);
		cout << random[n] << endl;
	}
		system("pause");
	return 0;
}

int generate(int min, int max)
{
	srand(GetTickCount());
	int random_integer;

	int range=(max-min)+1;

	random_integer = min+int(range*rand()/(RAND_MAX + 1.0));
	return random_integer;	
}

Dani AI

Generated

Two common mistakes were already identified in the thread: reseeding the RNG on every call (causing repeated values) and leaving the head pointer uninitialized (causing the access violation). Credit to for the RNG hint and to for spotting the initialization bug. Below are practical, durable improvements that make the generator safer, more portable, and easier to maintain.

Prefer C++11 <random> instead of rand/srand. A single, well-seeded engine (made static or thread_local) plus a distribution avoids reseeding mistakes and gives better randomness:

#include <random>

static std::mt19937& rng() {
    static std::random_device rd;
    static std::mt19937 gen(rd());
    return gen;
}

template<typename IntType>
IntType uniform_int(IntType lo, IntType hi) {
    std::uniform_int_distribution<IntType> dist(lo, hi);
    return dist(rng());
}

Use containers and RAII instead of raw arrays and naked new. Replace C-style arrays with std::vector<std::string> (so sizes are explicit), avoid magic indices, and validate bounds with countries.size(). For the linked list, prefer smart pointers (std::unique_ptr) or a std::list/std::vector to manage ownership and avoid manual delete. A cleaner factory approach is to have a function that constructs and returns an owned Profile (e.g., std::unique_ptr<ProfileType> make_random_profile()), then attach or push that into your container.

Final checks and debugging tips: initialize pointers (use nullptr in constructors), assert invariants before dereferencing, and use the debugger to inspect pointer values when an access violation happens. If code becomes multithreaded, make the RNG engine thread_local or otherwise ensure concurrent access is safe. These changes reduce subtle bugs and make the code easier to extend.

Recommended Answers

All 6 Replies

Call srand at the beginning of your program, not each time you call rand. You keep reseeding the random number generator, and because your program is running fast enough, the seed is always the same. This will result in the same "random" number every time.

thanks man, got it working but now a new problem.

int profile::randomNumGenerator(int min, int max)
{
	int random_integer;
	
	int range=(max-min)+1;

	random_integer = min+int(range*rand()/(RAND_MAX + 1.0));
	return random_integer;
}


void profile::createProfile()
{
	ProfileType *newProfile;
	ProfileType *profilePtr;
	newProfile = new ProfileType;

	string countries[50] = {"Afghanistan", "Australia", "Austria", "Bangladesh", "Barbados", 
		"Belgium", "Bhutan", "Burma", "Brazil", "Canada", "China", "Cuba", "Ethiopia", "France", 
		"Germany", "Greece", "Haiti", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", 
		"Italy", "Jamaica", "Japan", "Kenya", "Korea", "Kuwait", "Malaysia", "Morocco", "Mexico", 
		"Pakistan", "Qatar", "Russia", "Saudi Arabia", "Singapore", "South Africa", "Spain", 
		"Sri Lanka", "Sweden", "Switzerland", "Taiwan", "Thailand", "Turkey", "United Arab Emirates", 
		"United Kingdom", "United States of America", "Vietnam", "Zimbabwe"},
		ecoStatus[5] = {"EP", "P", "Avg", "R", "ER"},
		race[5] = {"A", "B", "C", "D", "E" },
		gender[2] = { "M", "F"},
		intellegence[3]={"Smart", "Normal", "Below normal"},
		height[3]={"Tall", "Average", "Short"},
		attitude[3]={"Meek", "Average", "Aggressive"},
		looks[3]={"Attractive", "Average", "Ugly"},
		luck[3]={"Lucky", "Average", "Unlucky"};

	if(_head == NULL)
	{
		newProfile->countryOfOrigin = countries[(randomNumGenerator(1,50)-1)];
		newProfile->economicStatus = ecoStatus[(randomNumGenerator(1,5)-1)];
		newProfile->race = race[(randomNumGenerator(1,5)-1)];
		newProfile->gender = gender[(randomNumGenerator(1,2)-1)];
		newProfile->intellegence = intellegence[(randomNumGenerator(1,3)-1)];
		newProfile->height = height[(randomNumGenerator(1,3)-1)];
		newProfile->attitude = attitude[(randomNumGenerator(1,3)-1)];
		newProfile->looks = looks[(randomNumGenerator(1,3)-1)];
		newProfile->luck = luck[(randomNumGenerator(1,3)-1)];
		newProfile->age = randomNumGenerator(1,100);
		newProfile->next = NULL;
		_head = newProfile;
	}
	else
	{
		profilePtr = _head;

		[B]while(profilePtr->next != NULL)[/B]
		{
			profilePtr = profilePtr->next;
		}
		newProfile->countryOfOrigin = countries[(randomNumGenerator(1,50)-1)];
		newProfile->economicStatus = ecoStatus[(randomNumGenerator(1,5)-1)];
		newProfile->race = race[(randomNumGenerator(1,5)-1)];
		newProfile->gender = gender[(randomNumGenerator(1,2)-1)];
		newProfile->intellegence = intellegence[(randomNumGenerator(1,3)-1)];
		newProfile->height = height[(randomNumGenerator(1,3)-1)];
		newProfile->attitude = attitude[(randomNumGenerator(1,3)-1)];
		newProfile->looks = looks[(randomNumGenerator(1,3)-1)];
		newProfile->luck = luck[(randomNumGenerator(1,3)-1)];
		newProfile->age = randomNumGenerator(1,100);
		newProfile->next = NULL;
		profilePtr->next = newProfile;

	}
}

Error is "Access violation reading location 0xcccccdf0.", which is the bold part while(profilePtr->next != NULL) in the code.

Do you see anything wrong with it? Is there any way I can improve this? By the way profile is a class, and ProfileType is a structure.

My educated guess is that you don't ever set _head to NULL.

yep, i had head == NULL instead of head = NULL.
thanks.
now, can i put the following code into another function say generateRandomProfile()?

newProfile->countryOfOrigin = countries[(randomNumGenerator(1,50)-1)];
		newProfile->economicStatus = ecoStatus[(randomNumGenerator(1,5)-1)];
		newProfile->race = race[(randomNumGenerator(1,5)-1)];
		newProfile->gender = gender[(randomNumGenerator(1,2)-1)];
		newProfile->intellegence = intellegence[(randomNumGenerator(1,3)-1)];
		newProfile->height = height[(randomNumGenerator(1,3)-1)];
		newProfile->attitude = attitude[(randomNumGenerator(1,3)-1)];
		newProfile->looks = looks[(randomNumGenerator(1,3)-1)];
		newProfile->luck = luck[(randomNumGenerator(1,3)-1)];
		newProfile->age = randomNumGenerator(1,100);
		newProfile->next = NULL;

if i do that how would i set profilePtr to the new Profile created? do i need pass profilePtr pointer as a refrenece or something?

>if i do that how would i set profilePtr to the new Profile created?
You're using new to allocate the new Profile, so just return that pointer from your function and assign it to profilePtr.

thanks man. It works now. Could you check if it everything is okay?
You see any places for improvement?

void profile::createProfile()
{
	ProfileType *newProfile;
	ProfileType *profilePtr;
	newProfile = new ProfileType;

	if(_head == NULL)
	{
		generateRandomProfile(newProfile);
		_head = newProfile;
	}
	else
	{
		profilePtr = _head;

		while(profilePtr->next != NULL)
		{
			profilePtr = profilePtr->next;
		}
		generateRandomProfile(newProfile);
		profilePtr->next = newProfile;
	}
}

void profile::generateRandomProfile(ProfileType* &newProfile)
{
	string	ecoStatus[5] = {"EP", "P", "Avg", "R", "ER"};

	newProfile->economicStatus = ecoStatus[(randomNumGenerator(0,4))];
	newProfile->next = NULL;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.