Solved

quality of rand() in VC++

Posted on 2002-04-30
6
810 Views
Last Modified: 2006-11-17
Anyone know what generator rand() in VC++6 (CRT) uses?

Is it good quality or not?  Is a RAND_MAX of 32767 far too low (as it would seem)?

A good reference on the subject would be welcome (specific to VC++ rand() please - I have a copy of TAOCP in front of me, enough random number generator theory for my lifetime :-)
0
Comment
Question by:IainHere
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Expert Comment

by:marcjb
ID: 6980535
Most of the random number generators shipped with compilers are not that good.  I was looking at a post on rec.games.programmer and one programmer noted that a test of VC++ 5 (I know you said you were using 6) gave a repeating cycle after several thousand numbers.  The poster noted that the source for rand was on the CD, so maybe they do that with version 6 also.  Anyway, two ways to limit the possibility of cycling:
Reseed after a set number of calls to the function.
Use a custom random number generator.

As for 32767 being too low, it really depends on what you are trying to do.  I am working with Linux, and gcc has RAND_MAX set to 2147483647.

Hope this helps,

Marc
0
 
LVL 2

Expert Comment

by:jonnin
ID: 6981493
Its not good, but better than many compilers' generators. Look up a battery of tests called diehard and test it (needs about 1 million numbers from the generator). Does statistics and predicibility tests on the numbers, bits, and more.

I have a home coded one for vc that passes all the tests, but its *very* slow, many computations / number...

if you want to try it, I can post it...


0
 
LVL 4

Author Comment

by:IainHere
ID: 6982845
I saw diehard, and thought that it needed 32 bit numbers - rand() is only 15 bit, isn't it?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:vbk_bgm
ID: 6983126
CryptoAPI also supports generating a random number (rather an array of bytes). The function is CryptGenRandom. If interested I can post the code to generate the random sequence of bytes.
0
 
LVL 2

Accepted Solution

by:
jonnin earned 50 total points
ID: 6983159
yea, it wants 32 bit numbers, but the tests are broken into chunks of bits, so it will partially work (the conclusion will be wrong, as the leading zeros will mess it up, but you can still figure it out and get results; the conclusion is just a summary of the chunk tests).
just stick rand into a 32 bit int to do this...

but I guess this is still invalid as it repeats at least every 32787 numbers, and dh wants a million, ~3 repeats in the set...

also, diehard was written for unix, so the high/low bit reports are backwards!

I would say that if you have to ask about the randomness of rand, you should not be using it.  A custom one is the way to go on important things. I think 15 bits is way too low, m$ probably did this because nt could orig run on several platforms and they picked lowest common hardware for some algorithms...  it crops up from time to time, whenever you see a 16 bit number used when a 32 should have been used...





0
 

Expert Comment

by:calum
ID: 6985729
i;ve been working on some genetic algorythm stuff that requires random mutations - and i;ve been using rand() succesfully. it;s not great but seems to do the job. must remember to seed the rand generator - i include time.h and use srand(time(NULL))

calum
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question