Solved

quality of rand() in VC++

Posted on 2002-04-30
6
791 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
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now