How to generate a random number in C?

Include #include srand(time(NULL)); int r = rand().

7 +1 for simplicity, but it is probably a good idea to emphasize that srand() should only be called once. Also, in a threaded application, you might want to make sure that the generator's state is stored per thread, and seed the generator once for each thread. – RBerteig May 5 '09 at 0:37.

The rand() function in returns a pseudo-random integer between 0 and RAND_MAX. You can use srand(unsigned int seed) to set a seed. It's common practice to use the % in conjunction with rand() to get a different range (though bear in mind that this throws off the uniformity somewhat).

Eg: /* random int between 0 and 19 */ int r = rand() % 20.

2 It is a common practice alright, but not the correct one. See this and this. – Lazer Aug 1 '10 at 7:33 @Lazer: That's why I said "though bear in mind that this throws off the uniformity somewhat".

– Laurence Gonsalves Aug 2 '10 at 7:00.

If you need better quality pseudo random numbers than what stdlib provides, check out Mersenne Twister. It's faster, too. Sample implementations are plentiful, for example here.

1: Looks cool but I was just making a guessing game. If I were going to use a random number generator in a business application then I would definitely use this. – Lucas McCoy Jun 1 '09 at 2:31.

Well, STL is C++, not C, so I don't know what you want. If you want C, however, there is the rand() and srand() functions: int rand(void); void srand(unsigned seed); These are both part of ANSI C. There is also the random() function: long random(void); But as far as I can tell, random() is not standard ANSI C.

A third-party library may not be a bad idea, but it all depends on how random of a number you really need to generate.

You are right - random() is not standard. – anon May 4 '09 at 22:20.

It seems to be a Question that gets Asked very Frequently. I see a couple just from the past few hours. FWIW, the answer is that yes, there is a stdlib call "rand"; this function is tuned primarily for speed and distribution, not for unpredictability.

Almost all built-in random functions for various languages and frameworks use this function by default. There are also "cryptographic" random number generators that are much less predictable, but run much slower. These should be used in any sort of security-related application.

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

STL doesn't exist for C. You have to call rand, or better yet, random. These are declared in the standard library header stdlib.h.

Rand is POSIX, random is a BSD spec function. The difference is that random returns a much more usable 32-bit random number, and rand typically returns a 16-bit number. The BSD manpages show that the lower bits of rand are cyclic and predictable, so rand is potentially useless for small numbers.

– anon May 4 '09 at 22:13 @Neil Butterworth: I did but I edited my post. – Lucas McCoy May 4 '09 at 22:15 1 @Neil - since all answers so far mention the STL, I suspect that the question was quick-edited to remove anunecessary reference. – Michael Burr May 4 '09 at 22:16 rand() isn't useless for small numbers - you can bitshift them out and use only the more random high bits if you really need to.

– Chris Lutz May 4 '09 at 22:20 @Chris, you can if the size of the random number is known, but if the required size of the random number changes during runtime (such as shuffling a dynamic array etc) it would be difficult to work around such a caveat. – dreamlax May 4 '09 at 22:30.

You want to use rand(). Note (VERY IMPORTANT): make sure to set the seed for the rand function. If you do not, your random numbers are not truly (pseudo)random.

This is very, very, very important. Thankfully, you can usually use some combination of the system ticks timer and the date to get a good seed.

Two points a) your random numbers are not "truly" random, no matter how you seed the generator. And b) it is very convenient to have the pseudo-random sequence always be the same in many circumstances - for testing, for example. – anon May 4 '09 at 22:19 2 if it's VERY IMPORTANT that your number be truly random, you shouldn't be using the rand() function.

– tylerl May 4 '09 at 22:19 The values from rand are not at all "truly" random no matter if you set the seed or not. Given a known seed the sequence is predictable. "Truly" random number generation is difficult.

There is no entropy involved with rand. – dreamlax May 4 '09 at 22:20 Okay, good points all; still, if you don't set your seed your numbers won't even be pseudo-random. – Paul Sonier May 4 '09 at 22:25 1 Of course they will - the generator is seeded for you by the library (probably to zero, but that's a valid seed).

– anon May 4 '09 at 22:29.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions