5 Replies - 62475 Views - Last Post: 30 October 2008 - 01:59 AM Rate Topic: ***** 1 Votes

#1 trabalas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6563
  • View blog
  • Posts: 30,701
  • 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);
}


Was This Post Helpful? 1

#8 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

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

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

Posted 30 October 2008 - 01:36 AM

View Postno2pencil, 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).

badjava
Was This Post Helpful? 0
  • +
  • -

#9 trabalas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6563
  • View blog
  • Posts: 30,701
  • 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.
Was This Post Helpful? 1
  • +
  • -

#11 trabalas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1