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;	
}

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.