?
Solved

quality of rand() in VC++

Posted on 2002-04-30
6
Medium Priority
?
815 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

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 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 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.

752 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