| | |
Realy weird random number generator bug
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2007
Posts: 47
Reputation:
Solved Threads: 1
Hello,
I made a program that uses a random number generator,
my generator looks like this:
here is the piece of code that calls the generator:
the weird thing is that the second call works well and gives a random number every time,but the first doesn't.
It gives the same number every time,and that number always equals
the function parameter(int highest) minus 1(when the parameter is 8
the function returns 7,when I changed it to 10 the function returned 9 etc.).
Can anyone explain why does it happen?
p.s.
I'm using VC++ 6.0
I made a program that uses a random number generator,
my generator looks like this:
C++ Syntax (Toggle Plain Text)
void randomize () { srand((unsigned)time(NULL)); } int random (int highest) { int randomnum; randomnum= int(highest*rand()/(RAND_MAX+1.0)); return randomnum; }
C++ Syntax (Toggle Plain Text)
int first,second; randomize(); first=random(8); second=random(11);
It gives the same number every time,and that number always equals
the function parameter(int highest) minus 1(when the parameter is 8
the function returns 7,when I changed it to 10 the function returned 9 etc.).
Can anyone explain why does it happen?
p.s.
I'm using VC++ 6.0
Because rand() isn't random at all perhaps?
The default algorithm is very simple, something along the lines of
http://en.wikipedia.org/wiki/Linear_...tial_generator
The first few iterations are likely to exhibit a degree of predictability which one might not expect.
See also http://c-faq.com/lib/notveryrand.html
The default algorithm is very simple, something along the lines of
http://en.wikipedia.org/wiki/Linear_...tial_generator
The first few iterations are likely to exhibit a degree of predictability which one might not expect.
See also http://c-faq.com/lib/notveryrand.html
•
•
Join Date: Jul 2007
Posts: 47
Reputation:
Solved Threads: 1
Hello Salem,
I know that rand() is a pseudorandom.
You probably missed this part of my program:
as you see I seed the generator with current time,therfore
the generator should start with a different number every time,
and the problem I described shouldn't happen.
I know that rand() is a pseudorandom.
You probably missed this part of my program:
C++ Syntax (Toggle Plain Text)
void randomize () { srand((unsigned)time(NULL)); } . . . . randomize();
as you see I seed the generator with current time,therfore
the generator should start with a different number every time,
and the problem I described shouldn't happen.
•
•
•
•
Hello Salem,
I know that rand() is a pseudorandom.
You probably missed this part of my program:
C++ Syntax (Toggle Plain Text)
void randomize () { srand((unsigned)time(NULL)); } . . . . randomize();
as you see I seed the generator with current time,therfore
the generator should start with a different number every time,
and the problem I described shouldn't happen.
Also, time isn't exactly the best seed for srand. Its common for the first number to always be the same when seeding with the current time. you could try a different method of seeding the generator, such as the one suggested by Narue here -
http://www.eternallyconfuzzled.com/a..._art_rand.aspx
Last edited by Bench; Aug 2nd, 2007 at 10:09 am.
¿umop apisdn upside down? > the generator should start with a different number every time,
And if the implementation of rand() just uses the most significant 16 bits of the 32 bit number, that as far as it is concerned, time() is a constant pretty much throughout the day.
So it can take a couple of iterations for all the rapidly changing least significant bits to have an impact on the pseudo random sequence.
If you really want to know, run the code in the debugger and then single step into the srand() and rand() calls in asm mode and figure out what it is doing.
And if the implementation of rand() just uses the most significant 16 bits of the 32 bit number, that as far as it is concerned, time() is a constant pretty much throughout the day.
So it can take a couple of iterations for all the rapidly changing least significant bits to have an impact on the pseudo random sequence.
If you really want to know, run the code in the debugger and then single step into the srand() and rand() calls in asm mode and figure out what it is doing.
•
•
Join Date: Jul 2007
Posts: 47
Reputation:
Solved Threads: 1
Thanks again for all the help,
just wanted to ask one more question:
In the link that Bench posted(http://www.eternallyconfuzzled.com/a..._art_rand.aspx) there is a piece of code that looks like this:
I just wanted to ask what meaning the "U" after the "2" has?
(I guess it means "unsigned",but why does it matter,why just not write:
UCHAR_MAX+2)
just wanted to ask one more question:
In the link that Bench posted(http://www.eternallyconfuzzled.com/a..._art_rand.aspx) there is a piece of code that looks like this:
C++ Syntax (Toggle Plain Text)
for ( i = 0; i < sizeof now; i++ ) { seed = seed * ( UCHAR_MAX + 2U ) + p[i]; return seed; }
(I guess it means "unsigned",but why does it matter,why just not write:
UCHAR_MAX+2)
Last edited by Arctic wolf; Aug 2nd, 2007 at 10:09 pm.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: WAnt Some Advice...
- Next Thread: serious help calculating averages
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






