# How to use rand() in a certain range of Numbers?

Page 1 of 1

## 5 Replies - 66712 Views - Last Post: 30 October 2008 - 01:59 AMRate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=69684&amp;s=d179371ed4943451bdbdc00fd5e89c9d&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 trabalas

• New D.I.C Head

Reputation: 0
• Posts: 18
• Joined: 14-October 08

# How to use rand() in a certain range of Numbers?

Posted 30 October 2008 - 01:20 AM

Hello.I am new at C programming and need some help.

I would like to know if there is any formula to use rand() in a specific range of numbers.

I have read all the rand()/srand() topics but the range of numbers used is always 1 to 10 or 1 to 20

For example if we want a range 128-254 how it will be like??

I have done it like this
```a=rand()%254+128;
```
but it doesnt work cause it generates numbers higher than 300

Thnx for your time/Sry for my bad english.

Is This A Good Question/Topic? 0

### #7 no2pencil

• Professor Snuggly Pants

Reputation: 6644
• Posts: 30,940
• Joined: 10-May 07

## Re: How to use rand() in a certain range of Numbers?

Posted 30 October 2008 - 01:28 AM

The following will generate a random number between MIN & MAX a random number of times between 1 & 13.

```#include <stdio.h>
#include <time.h>

#define MIN 0
#define MAX 100

int getrand(int min, int max);

int main(void) {
int p=0, i=0;
srand(time(NULL));

for(i=0;i<getrand(1,13); i++) p=getrand(MIN,MAX);
printf("Random value : p\n",p);

return 0;
}

int getrand(int min,int max){
return(rand()%(max-min)+min);
}

```

• Lux Ex Tenebris

Reputation: 14
• Posts: 540
• Joined: 30-October 08

## Re: How to use rand() in a certain range of Numbers?

Posted 30 October 2008 - 01:36 AM

no2pencil, on 30 Oct, 2008 - 01:28 AM, said:

The following will generate a random number between MIN & MAX

Hey n02, are u a java prgmr also? I am trying to find a way sticking as close as possible to the simple built in random() mthod to pick randomly from a set of numbers like (1,2,5,7,25).

### #9 trabalas

• New D.I.C Head

Reputation: 0
• Posts: 18
• Joined: 14-October 08

## Re: How to use rand() in a certain range of Numbers?

Posted 30 October 2008 - 01:52 AM

Thanks for the help.The code above provides random numbers in a range of MIN-MAX (0-100 in this case) but i dont really understand the 1-13 thing.

### #10 no2pencil

• Professor Snuggly Pants

Reputation: 6644
• Posts: 30,940
• Joined: 10-May 07

## Re: How to use rand() in a certain range of Numbers?

Posted 30 October 2008 - 01:56 AM

The return value from the function getrand is random number between the values given. So if you use it in a for loop, like in the example I gave, it will run the loop a random number of times. In this situation, it's between 1 & 13. It'll give you a more random result.

You don't have to use it. It just kind of shows dual usage.

### #11 trabalas

• New D.I.C Head

Reputation: 0
• Posts: 18
• Joined: 14-October 08

## Re: How to use rand() in a certain range of Numbers?

Posted 30 October 2008 - 01:59 AM

Ohh ye you are right.Thanks again for your time!!