Solved

quality of rand() in VC++

Posted on 2002-04-30
6
812 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
Independent Software Vendors: 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

Industry Leaders: 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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

691 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