views:

6245

answers:

8

Is there a function or will I have to use a third party library?

A: 

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.

dreamlax
Who mentioned the STL?
anon
@Neil Butterworth: I did but I edited my post.
Lucas McCoy
@Neil - since all answers so far mention the STL, I suspect that the question was quick-edited to remove anunecessary reference.
Michael Burr
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
@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
+1  A: 

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.

Chris Lutz
You are right - random() is not standard.
anon
A: 

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.

McWafflestix
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
if it's VERY IMPORTANT that your number be truly random, you shouldn't be using the rand() function.
tylerl
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
Okay, good points all; still, if you don't set your seed your numbers won't even be pseudo-random.
McWafflestix
Of course they will - the generator is seeded for you by the library (probably to zero, but that's a valid seed).
anon
I don't particularly count "pseudo-random according to a known algorithm and a known seed" to be fulfilling the qualifications for any level of "randomness". :-)
McWafflestix
Ah, but known algorithm/known seed is essential to debugging any program that uses random numbers. It isn't unusual to log the seed used along with a simulation run so that it can be recreated for more detailed analysis. Not calling srand() at all is equivalent to calling srand(1).
RBerteig
+5  A: 

The rand() function in <stdlib.h> 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;
Laurence Gonsalves
@Laurence Gonsalves: It is a *common practice* alright, but not the correct one. See [this](http://stackoverflow.com/questions/2999075/generate-a-random-number-within-range/2999130#2999130) and [this](http://stackoverflow.com/questions/288739/generate-random-numbers-uniformly-over-entire-range/288869#288869).
Lazer
@Lazer: That's why I said "though bear in mind that this throws off the uniformity somewhat".
Laurence Gonsalves
+1  A: 

Is there a FAQ entry for this question? 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.

tylerl
+1  A: 

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

geofftnz
+18  A: 
#include <time.h>
#include <stdlib.h>

srand(time(NULL));
int r = rand();
Łukasz Lew
+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
+2  A: 

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.

MH114
+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