Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

quality of rand() in VC++

Posted on 2002-04-30
6
Medium Priority
?
820 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

604 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