1

Possible Duplicate:
Filling an array with random numbers from 1 to 10^10 in C or C++

Hello,

Simple question; I am looking for a way to get a higher random value than rand() gives me. rand() gives 32767 max. (http://msdn.microsoft.com/en-us/library/398ax69y%28v=vs.80%29.aspx) I've tried redefining RAND_MAX, but I guess the compiler dosn't accept redefining, because it's within the same boundary as default.

I basically want a random value that fits in an unsigned integer (generating a SSRC number in an RTP packet). I really don't want collisions! Is there any better win32 function than rand()?

Thanks in advance

Community
  • 1
  • 1
KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199

4 Answers4

6

Yes -- RAND_MAX is just telling you the maximum that's designed into the generator. Changing it doesn't affect how the generator works.

The easiest way to get a 32-bit value from it is probably to call rand() twice in a row, and and put the two values together:

unsigned SSRC = rand() | rand << 16;

One typical way of seeding rand() is with time:

srand((unsigned)time(NULL));

For an SSRC, you do not want to do this -- time typically only has one-second granularity, so this would (almost) guarantee that any two processes started close to the same time would cause collisions.

In reality, randomness doesn't matter nearly as much as uniqueness. Another possibility would be (for example) to take your machine's IP address, the process ID, and the time and XOR the three together. The result isn't particularly random at all, but is sufficiently unique for this kind of task.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • That's going to leave two bits always zero (since the C RNG generates a minimum of 15 bits, calling it twice yields only 30, not 32). – Ben Voigt Jan 28 '11 at 21:06
  • 1
    @Ben: quite true -- but only rarely relevant. If you're *really* convinced it's a problem, you can use three calls to `rand` instead, but I doubt you gain much by doing so. – Jerry Coffin Jan 28 '11 at 21:12
  • @Jerry Coffin: A 'random number generator' that always generates the same value for a specific bit is not a random number generator, it's a joke. If you want to use rand() to generate 32 (or even just 31) random bits, you absolutely have to call it three times. – TonyK Jan 28 '11 at 21:16
  • 1
    @TonyK: The point is that for an SSRC, you *don't* really need any great degree of randomness at all. – Jerry Coffin Jan 28 '11 at 21:57
  • for rand() | rand() << 16; the first and second rand() call will generate different numbers right? so it's not the same 15bit sequence twice – KaiserJohaan Jan 28 '11 at 22:07
  • @KaiserJohaan: Yes, that's correct. – Jerry Coffin Jan 28 '11 at 22:12
  • in this case RAND_MAX has only 15 bits so the result should be `rand() << 30 | rand() << 15 | rand();` – phuclv Jan 23 '15 at 18:41
3

Use Boost.Random (which is also available as <random> if you have VC++ 2010). Note that this will become a built-in part of the C++ Standard in the near future.

There are some good examples on the Boost site, including Generating integers in a range.

Nate
  • 18,752
  • 8
  • 48
  • 54
  • std::random? Can you set a max number to random to? – KaiserJohaan Jan 28 '11 at 21:08
  • 1
    Yes. (Technically I should have said `` since the class names are more like `std::mt19937` and `std::uniform_int<>`.) You have a choice of random generators. For example, `mt19937` produces integers in the range [0, (2^32)-1]. You can restrict this range as you wish and, using `uniform_int`, guarantee that the resulting random numbers are uniformly distributed in your desired range. – Nate Jan 28 '11 at 21:13
1

How about rand()<<15 + rand() ? It can be as big as you want.

Xi 张熹
  • 10,492
  • 18
  • 58
  • 86
1

Changing RAND_MAX is not going to change the behavior of the rand() function, which is defined in the C runtime; it's just a convenient macro for you to know the range of output produced by rand().

If you want high-quality random numbers suitable for use with cryptography, use CryptGenRandom; see the full example at the bottom under Community Content for how to use it.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589