Click to See Complete Forum and Search --> : Looping count of random generated numbers


process
10-03-2008, 09:37 AM
Hi... i'm a little confused. Main requires user input - requesting the amount of random numbers wanted... this value is passed as a parameter to the following function.



int range(int x)
{

int counter = 0;


while (counter < x)
{
cout<<"here.... range id = "<< counter+1 << endl;

randomize();
int random = rand();
float ran_2 = (random*1.00)/RAND_MAX;
int ran_3 = ran_2*x;
cout<< "random number generated ="<<ran_3<<endl;
counter++;
}
return(0);
}



the program will return every random number wanted as the same number...
press 5 wanted...
& the 5 random numbers will all be the same =/

why isn't the loop generating new random numbers?

drkybelk
10-03-2008, 10:50 AM
rand() delivers pseudo random numbers
You need to initialize the generator using srand(int seed) with for example the system time
I suspect your randomize() function attempts something like that but uses the same integer all the time....

hendrixj
10-03-2008, 01:03 PM
Pull the randomize call out of the function and put it in main. I don't know what randomize is, but I assume it is calling srand with a specific seed. If so, everytime you call it, it starts the sequence over. So, you are stuck on 1.

If srand is being called with the same seed, the sequence will always be the same from run to run. In that case, you may want to use "srand(time(NULL))" to get an unpredictable start point for the the pseudo-random sequence.

process
10-03-2008, 01:12 PM
i see where ur coming from .... but applying the comp time to randomise the numbers is errrm

int counter = 0;
time_t t;

while (counter < x)
{
cout<<"here.... range id = "<< counter+1 << endl;

srand((unsigned) time(&t));
int random = rand();
float ran_2 = (random*1.00)/RAND_MAX;
int ran_3 = ran_2*x;
cout<< "random number generated ="<<ran_3<<endl;
counter++;
}
return(0);
}


going wrong somewhere =/

hendrixj
10-03-2008, 01:21 PM
What's "errrm"? If you're saying you get a compilation error, it's probably becuase you didn't include <ctime>.
Don't put the srand call in the while loop, you'll get a "more random" sequence by calling srand once and letting the algorithm follow its sequence.

jonnin
10-03-2008, 03:23 PM
what exactly are you trying to do.
You take a random %, I guess:
random/randmax is a random % and the X 1.0 does nothing ??
you multiply this by a value (constant???) named X that we do not know much about... but it looks to be a constant at least for the scope of the loop.
and you then print that result as a random number.
Depending on what X is, you could get some odd results from this. I.E the smaller X is, and here I think you said 5 or 10, the worse the results will be.

jonnin
10-03-2008, 03:28 PM
int main()
{
int counter = 0;
time_t t;
srand((unsigned) time(&t));
int x = 10;

while (counter < x)
{
cout<<"here.... range id = "<< counter+1 << endl;


int random = rand();
float ran_2 = (random*1.00)/(double)RAND_MAX;
int ran_3 = ran_2*x;
cout<< "random number generated ="<<ran_3<<endl;
counter++;
}
return(0);
}

----------------------------
here.... range id = 1
random number generated =4
here.... range id = 2
random number generated =0
here.... range id = 3
random number generated =1
here.... range id = 4
random number generated =7
here.... range id = 5
random number generated =1
here.... range id = 6
random number generated =6
here.... range id = 7
random number generated =0
here.... range id = 8
random number generated =2
here.... range id = 9
random number generated =5
here.... range id = 10
random number generated =8
Press any key to continue . . .


Is this acceptable?

process
10-04-2008, 06:56 AM
what exactly are you trying to do.
You take a random %, I guess:
random/randmax is a random % and the X 1.0 does nothing ??
you multiply this by a value (constant???) named X that we do not know much about... but it looks to be a constant at least for the scope of the loop.
and you then print that result as a random number.
Depending on what X is, you could get some odd results from this. I.E the smaller X is, and here I think you said 5 or 10, the worse the results will be.

It is the start of a genetic algorithm. User enters amount of population (amount of wanted random numbers) X defines this amount. The * 1.0 call isn't always needed - depends on the compiler but in my case it was...it, as far as i know, corrects the int to float conversion to provide a whole number(?).

next I gotta make a function that converts a number into a binary string

oh & thanks for your help... as said i had to take out the srand time from the loop =)

thank u thank u