# I didnt notice this about random :(

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-21-2009
Nathan the noob
I didnt notice this about random :(
With the rand() function rand keeps going higher with the number ex: 4 then 15 then 27 then 35 then 41 then 61 then 85 then 99 then 1 < until it reaches the number its max is any way to make that obsolete or is it just the way rand works

even if u first have max as 200 and min as 1 and it comesx out with 56 if u do 100 and min 1 it will be higher
Code:

```#include<iostream> #include<cmath> #include<ctime> using namespace std; int main() {     int random;     int max;     int min;     while(random)     {                 cout << "Enter a max: ";                 cin >> max;                 cout << "Enter a min: ";                 cin >> min;     srand(time(NULL));                random =(rand() % max) + min;     cout <<"Hey the numbereno is " << random << "\n";     } }```
also im still looking for a sure way that max will always be the highst number reachable and min will always be the lowest cause for some reason if 2000 is max and min then the answer is liek 3759
• 02-21-2009
tabstop
Well, if you use % max, you will get a number from 0 to (max-1). If you add min to that, you will get a number from min to (max-1)+min. If you want to go from min to max, then you need to change what you % by, so that at the end (n-1)+min = max. You should be able to solve that for n.
• 02-21-2009
cpjust
You're only supposed to call srand() once, not every time you go through the loop.
• 02-21-2009
jEssYcAt
Only use srand(time(NULL)); one time in your program, generally when it starts (in your program, I would put it between int min; and while(random)

srand() is meant to seed rand()'s algorithm once and then the sequence you get from rand() will be random. When you call srand() again, you start the sequence over, which defeats the way rand() works.
• 02-21-2009
iMalc
It never ceases to amaze me, how many times per week people make this same mistake. Does nobody ever RTFM any more?
• 02-21-2009
Nathan the noob
Im aware about the srand thing this is old coding from like 2 months ago was using to test :P.

iMalc wat mistake?

And nvm il just atempt to rite my own random number class
• 02-22-2009
vart
Quote:

Originally Posted by Nathan the noob
Im aware about the srand thing this is old coding from like 2 months ago was using to test :P.

iMalc wat mistake?

And nvm il just atempt to rite my own random number class

rand()%max

will give you values from 0 to max-1

rand()%max+min

will give values from min to min+max-1
• 02-22-2009
VirtualAce
Writing your own pseudo-random number generator is not all that simple unless you are a big time math guru. There are several open-source algos on the net and there are many books on Amazon for this very thing.
• 02-22-2009
iMalc
Quote:

Originally Posted by Nathan the noob
Im aware about the srand thing this is old coding from like 2 months ago was using to test :P.

iMalc wat mistake?

And nvm il just atempt to rite my own random number class

The mistake is what cpjust pointed out. You seem to think this was just someone's coding style advice or something like that, but what you've been told about is the actual cause of the oddity you are seeing in the numbers generated. You fix it by moving the srand to before the loop.
I guess in your defence, many articles about rand don't stress enough that you're not supposed to call srand more than once, unless you want to repeat a previous sequence.

You wont be any better off trying to write your own pseudo random number generator.
• 02-23-2009
brewbuck
Quote:

Originally Posted by iMalc
It never ceases to amaze me, how many times per week people make this same mistake. Does nobody ever RTFM any more?

It never ceases to amaze me that modern C libraries don't automatically seed the RNG from time() in the first place, so that people wouldn't have to understand srand().

If you really want repeatability, you can always call it yourself.
• 02-23-2009
iMalc
Quote:

Originally Posted by brewbuck
It never ceases to amaze me that modern C libraries don't automatically seed the RNG from time() in the first place, so that people wouldn't have to understand srand().

If you really want repeatability, you can always call it yourself.

I also think that would have been a good way for it to have been done.
• 02-23-2009
CornedBee
It violates the zero cost principle :)
• 02-23-2009
brewbuck
Quote:

Originally Posted by CornedBee
It violates the zero cost principle :)

Not necessarily... You could have this:

Code:

```int rand() {     static int initted = 0;     if(!initted)     {         srand(time(NULL));         initted = 1;     }     ... }```
• 02-23-2009
CornedBee
The idea would work, but your implementation doesn't. srand() must have access to that initted variable and set it on call, or ... well, you can easily guess what happens when I call srand() with my own seed before ever calling rand(). :D
• 02-23-2009
brewbuck
Quote:

Originally Posted by CornedBee
The idea would work, but your implementation doesn't. srand() must have access to that initted variable and set it on call, or ... well, you can easily guess what happens when I call srand() with my own seed before ever calling rand(). :D

Yeah -- good catch.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last