Generating a Random Integer Within a Range 
Author Message
 Generating a Random Integer Within a Range

What's the best way to generate a random integer from and including an
integer 'from' up to and including an integer 'to'?

The below code works unless 'from' is 0 in which case values equal to 'to'
will never be generated.

So what's the best/correct way to do this?

Mike

int
randint(int from, int to)
{
    float ffrom = (float)from;
    float fto = (float)to;

    return from + (int)(fto * rand() / (RAND_MAX + ffrom));

Quote:
}



Sat, 29 Oct 2005 12:34:59 GMT  
 Generating a Random Integer Within a Range

Quote:
> What's the best way to generate a random integer from and including an
> integer 'from' up to and including an integer 'to'?

The C FAQ has a technique for getting values in range 0...(N-1).
So, let N = TO - FROM + 1 and add FROM to the value produced.
--
"Give me a couple of years and a large research grant,
 and I'll give you a receipt." --Richard Heathfield


Sat, 29 Oct 2005 12:39:36 GMT  
 Generating a Random Integer Within a Range



Quote:
> What's the best way to generate a random integer from and including an
> integer 'from' up to and including an integer 'to'?

> The below code works unless 'from' is 0 in which case values equal to 'to'
> will never be generated.

> So what's the best/correct way to do this?

> Mike

> int
> randint(int from, int to)
> {
>     float ffrom = (float)from;
>     float fto = (float)to;

>     return from + (int)(fto * rand() / (RAND_MAX + ffrom));
> }

#include <stdlib.h>

/* Generates random integer value in range */
/* 'lo' to 'hi' (inclusive)                */
int gen_random_int(int lo, int hi)
{
/*  Formula from C FAQ 13.16                         */
/*  ( http://www.eskimo.com/~scs/C-faq/q13.16.html ) */

    return (int)((double)rand()
        / ((double)RAND_MAX + 1)
            * (++hi - lo) + lo);

Quote:
}

-Mike


Sat, 29 Oct 2005 15:09:14 GMT  
 Generating a Random Integer Within a Range

Quote:


>> What's the best way to generate a random integer from and including an
>> integer 'from' up to and including an integer 'to'?

> The C FAQ has a technique for getting values in range 0...(N-1). So, let
> N = TO - FROM + 1 and add FROM to the value produced.

Mmm, busted.

Thanks,
Mike



Sat, 29 Oct 2005 16:08:47 GMT  
 Generating a Random Integer Within a Range

Quote:
>What's the best way to generate a random integer from and including an
>integer 'from' up to and including an integer 'to'?

There is no best way to generate random numbers.  Any method based on
rand() is likely to be among the worst (typically, rand() is (poorly)
designed for speed, not for the quality of the generated numbers).

Quote:
>The below code works unless 'from' is 0 in which case values equal to 'to'
>will never be generated.

>So what's the best/correct way to do this?

A correct way is described by the FAQ.  Since it's based on rand(), it
may or may not be suitable for your application's purposes.

If you want a quality random generator, use one that is guaranteed to be
suitable for your purposes.  There is plenty of literature addressing the
issue.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Sat, 29 Oct 2005 17:47:46 GMT  
 Generating a Random Integer Within a Range

Quote:

> What's the best way to generate a random integer from and including an
> integer 'from' up to and including an integer 'to'?

> The below code works unless 'from' is 0 in which case values equal to 'to'
> will never be generated.

> So what's the best/correct way to do this?

> Mike

> int
> randint(int from, int to)
> {
>     float ffrom = (float)from;
>     float fto = (float)to;

>     return from + (int)(fto * rand() / (RAND_MAX + ffrom));
> }

You can adapt this as a function:

/* generates pseudorandom numbers
    in the range: start to (range - 1) + start */

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

int main(void)
{
   int x, idx, rndnum, range = 501, start = 200;

   /* srand() randomizes the seed each time the program
      is run, in this case with the system time */

   srand((unsigned int) time(0));

   /* rand() generates a pseudorandom int
      in the range 0 to MAX_RAND. When used
      with the % operator and the range value
      it produces a result between 0 and range */

   for(idx = 1; idx <= 10; idx++)
   {
     x = rand() % range;
     rndnum = x + start;
     printf("%d\n", rndnum);
   }

   return 0;

- Show quoted text -

Quote:
}



Mon, 31 Oct 2005 08:11:01 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Generating a random number in a range...???

2. HELP: Random Numbers within a range

3. Test if input is integer within range

4. Any good methods for generating pseudo random integer?

5. Generating Unique random integers

6. How to generate random integer number??

7. Generating unique random integers

8. random range

9. Selecting Random Number From a Range

10. HELP w/ Random # from array not range?

11. Random nnumber between a range

12. Random number in range

 

 
Powered by phpBB® Forum Software