Question: > 
> **Possible Duplicates:**  
> 
> [How to generate a random number in C?](https://stackoverflow.com/questions/822323/how-to-generate-a-random-number-in-c)  
> 
> [implementation of rand()](https://stackoverflow.com/questions/1167253/implementation-of-rand)  
> 
> [Generating random terrain in Blender3D](https://stackoverflow.com/questions/31834/generating-random-terrain-in-blender3d) 
> 
> 
> 

I need high quality random numbers in C, but I have no idea what to really do. I need to be able to get numbers from 1-100. Any help or maybe point me to where I can find help.

Answer: 
This is the simplest method of producing uniformly distributed random numbers in C:

**Step 1.** Be sure to include the standard library header to get the necessary function prototypes

```
#include <stdlib.h>

```

**Step 2.** Seed the random number generator using `srand()`. The seed determines where the random numbers start. The sequence of random numbers will always be exactly the same for a given seed. This allows you to have random, yet reproducible results. If you don't need it to be reproducible, a good thing to seed with is the current time, so that the random sequence will be different on each run.

```
srand(time(NULL));

```

(be sure to include time.h if you do this). Also, *only seed the generator once per program run* unless you are generating a huge number (millions or billions) of random numbers. Seeding frequently makes the sequence *less* random.

**Step 3.** Get your random number.

```
rand()

```

This function returns a random number between 0 and RAND\_MAX, which is a macro that is defined as a rather large integer.

**Step 4.** Get your random number into the range you want. The general formula for doing so is this:

```
int random_number = rand() % range + min;

```

Where range is how many (consecutive) numbers you want to choose from, and min is the smallest of these. So to generate a number between 1 and 100, range is 100 and min is 1:

```
int random_number = rand() % 100 + 1;

```

Some people object to this formula because it uses the low-order bits of the number given by rand(), and in older implementations of software pseudo-random number generators these were often less random than the high order bits, but on any modern system this method should be perfectly fine.
<http://mathworld.wolfram.com/RandomNumber.html>

This article explains the basics to creating your own random number generator that will outperform the standard C library function if you find it lacking in distribution. It produces a better spread and hence a more random number.

This is critical if you hope to produce something that can not be reverse engineered, like for poker sites.