**Answiki**on 09/16/2022 at 08:19:29 PM UTC

# In C, how to pick a random number in an interval?

**Answiki**on 09/16/2022 at 08:18:10 PM UTC

In C, the `rand()`

function is used to generate a pseudo-random number between 0 and `RAND_MAX`

. `RAND_MAX`

is a macro defined in `stdlib.h`

. To generate a number in a given range (between `min`

and `max`

), we can use one of the following solutions:

__Integer :__

```
// Pick a random number between min and max (included)
int x = min + rand() % (max + 1 - min);
```

__Float :__

```
// Pick a random floating point number between min and max (included)
float x = min + (float)rand() / ((float)RAND_MAX/(max-min));
```

When using these codes, remember to initialize the generator seed with `srand()`

so as not to generate the same draws at each run. For more details, check this question :

**Answiki**09/16/2022 at 08:19:29 PM

**Answiki**09/16/2022 at 08:19:06 PM

**Answiki**09/16/2022 at 08:18:55 PM

**Answiki**09/16/2022 at 08:18:48 PM

**Answiki**09/16/2022 at 08:18:16 PM

**Answiki**on 09/16/2022 at 08:18:10 PM

In C, the `rand()`

function is used to generate a pseudo-random number between 0 and `RAND_MAX`

. `RAND_MAX`

is a macro defined in `stdlib.h`

. To generate a number in a given range (between `min`

and `max`

), we can use one of the following solutions:

__Integer :__

```
// Pick a random number between min and max (included)
int x = min + rand() % (max + 1 - min);
```

__Float :__

```
// Pick a random floating point number between min and max (included)
float x = min + (float)rand() / ((float)RAND_MAX/(max-min));
```

When using these codes, remember to initialize the generator seed with `srand()`

so as not to generate the same draws at each run. For more details, check this question :

**Answiki**on 09/16/2022 at 08:15:52 PM

In C, the `rand()`

function is used to generate a pseudo-random number between 0 and `RAND_MAX`

. `RAND_MAX`

is a macro defined in `stdlib.h`

. To generate a number in a given range (between `min`

and `max`

), we can use one of the following solutions:

__Integer :__

```
// Pick a random number between min and max (included)
int x = min + rand() % (max + 1 - min);
```

__Float :__

```
// Pick a random floating point number between min and max (included)
float x = min + (float)rand() / ((float)RAND_MAX/(max-min));
```

When using it, remember to initialize the generator seed with `srand()`

so as not to generate the same draws at each run. For more details, check this question :

**Answiki**on 09/16/2022 at 08:15:07 PM

`rand()`

function is used to generate a pseudo-random number between 0 and `RAND_MAX`

. `RAND_MAX`

is a macro defined in `stdlib.h`

. To generate a number in a given range (between `min`

and `max`

), we can use one of the following solutions:

__Integer :__

```
// Pick a random number between min and max (included)
int x = min + rand() % (max + 1 - min);
```

__Float :__

```
// Pick a random floating point number between min and max (included)
float x = min + (float)rand() / ((float)RAND_MAX/(max-min));
```

When using it, remember to initialize the generator seed with `srand()`

so as not to generate the same draws at each run.

**Answiki**09/16/2022 at 08:11:11 PM

# | ID | Query | URL | Count |
---|---|---|---|---|

0 | 8627 | pick random number at interval | https://en.ans.wiki/6674/in-c-how-to-pick-a-random-number-in-an-interval | 1 |