tags:

views:

919

answers:

9

I've used

#include<stdlib>
#include<time>
using namespace std;
srand((unsigned)time(0));
int n=(rand()>>8)%4;

but what other random functions are there, or what other function could be used as random number generators?

EDIT: I don't really have a particular reason for asking this question, I just wanted to know if C++ had any other random functions.

A: 

Time is usually the most random operation that is also cheap to perform, but it's still possible to predict.

If you want true randomness, using some kind of external input is your only solution.

Quantum Random Bit Generator is one service that provides such data.

Andrei Krotkov
Time is not all that random. I wrote some online gambling software once and we weren't allowed to use a time-based random generator to decide what cards to deal. Turns out, it's quite easy to guess roughly what time the server is running on based on the hand you are dealt.
rein
True. I'll delete that line.
Andrei Krotkov
If you're willing to use assembly, all pentium or better have a thermal diode with an instruction to get at it.
Joshua
+5  A: 

Not strictly C++, but Windows specific:

CryptGenRandom

I'm sure all operating systems have their equivalent cryptographically secure random generator functions.

rein
A: 

Random gives you a good random number at uniform distribution and does a pretty good job at that.

Anything else would mean that you want to actually skew the distribution.

For example, using Microsoft's GUIDs generator would give you a random id that is less likely to be repeated and takes into account things like time and computer.

Uri
GUIDs are not random
anon
I think he's looking for cryptographically secure functions, not just even distribution. The questions is not very clear though.
Zifre
@Uri Amd what is this "Random" you refer to in "Random gives you..."?
anon
@Neil: rand... Too much Java thinking, sorry.
Uri
Well, in that case you are wrong about it being good - many rand() implementations are pretty piss-poor.
anon
I'm sure you mean a uniform distribution. A standard distribution follows a bell curve, whereas a uniform distribution gives a result within the range all with exactly equal probability.
TokenMacGuy
Yes, I'll fix that. I was translating badly from Hebrew. We use "standard" distribution for uniform, and "normal" distribution for the bell curve.
Uri
+15  A: 
  • Boost Random Number Library offers a broad range of generators (quality vs performance) and some typical random distributions. Everything rather nice and straightforward to use.
  • If you want some other methods/libraries - then google for cryptographic random numbers, also you can use this document as a reference.
  • Don't invent your own solutions unless you are an expert/researcher in the field/etc, take advantage of already existing solutions which were usually written by Smart People, and thoroughly examined by other Smart People.
Anonymous
+1  A: 
int unixrand()
{
   int x;
   int f = open("/dev/random", O_RDONLY);
   if (f < 0) return -1; /* Error */
   if (sizeof(x) != read(f, &x, sizeof(x))) {
       close(f);
       return -1;
   }
   close(f);       
   if (x < 0) x = -x;
   return x;
}
Joshua
Why the check for x < 0? Also, /dev/urandom would be a better choice, unless you want really random data (for cryptography etc). /dev/random is too valuable a resource :)
Paggas
I'm making a function that returns any positive random x like the C standard library function. Yes, /dev/urandom is better for most cases.
Joshua
+13  A: 

The rand() and srand() functions are all the C++ Standard specifies. And if it comes to writing your own, be aware of what John von Neumann said:

"Anyone who considers arithmetical methods of producing random digits is of course in a state of sin"

anon
+1  A: 

(Cross-posting from an answer I just wrote to a similar question)

Have a look at ISAAC (Indirection, Shift, Accumulate, Add, and Count). Its uniformly distributed and has an average cycle length of 2^8295.

It's fast too, since it doesnt involve multiplication or modulus.

geofftnz
A: 

Bruce Schneier and John Kelsey wrote a random number generator you may be interested in. Rather, it's a seed generator. Even though Yarrow is no longer supported, you may be interested in how it gathers entropy.

OpenSSL has an API that is relatively easy to access and pretty portable. And Mozilla comes with a decent API that wraps whatever the OS offers.

Personally, though, I generally use Boost.Random, which was already suggested.

Max Lybbert
+4  A: 

This code is pretty efficient. Although users may begin to notice a pattern after a few iterations.

int FastRandom()
{
  return 10;
}
justinhj
LOL!! good one!
fengshaun
You just copied number 221 :) http://xkcd.com/221/
Not Sure
It wasn't conscious, but yeah, good cartoon and I probably saw it!
justinhj