5

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.

  • 4
    Please make the question more clear; do you mean cryptographically secure (hard to predict) or just good distribution? – Zifre May 04 '09 at 22:07

9 Answers9

16
  • 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
  • 18,162
  • 2
  • 41
  • 64
15

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

int FastRandom()
{
  return 10;
}
justinhj
  • 11,147
  • 11
  • 58
  • 104
14

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"

5

Not strictly C++, but Windows specific:

CryptGenRandom

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

rein
  • 32,967
  • 23
  • 82
  • 106
2
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
  • 40,822
  • 8
  • 72
  • 132
  • 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 May 04 '09 at 21:57
  • 1
    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 May 04 '09 at 22:15
1

(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.

Community
  • 1
  • 1
geofftnz
  • 9,954
  • 2
  • 42
  • 50
0

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
  • 19,717
  • 4
  • 46
  • 69
-2

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
  • 88,451
  • 51
  • 221
  • 321
  • I think he's looking for cryptographically secure functions, not just even distribution. The questions is not very clear though. – Zifre May 04 '09 at 21:53
  • @Uri Amd what is this "Random" you refer to in "Random gives you..."? –  May 04 '09 at 22:02
  • @Neil: rand... Too much Java thinking, sorry. – Uri May 04 '09 at 22:08
  • 3
    Well, in that case you are wrong about it being good - many rand() implementations are pretty piss-poor. –  May 04 '09 at 22:10
  • 1
    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. – SingleNegationElimination May 04 '09 at 23:16
  • 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 May 05 '09 at 00:11
-4

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
  • 5,556
  • 3
  • 33
  • 36
  • 2
    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 May 04 '09 at 21:44