# Thread: Generate Random Number

1. ## Generate Random Number

Hi everybody

Code:
```int intrnd ()
{
double const a    = 16807;      /* ie 7**5 */
double const m    = 2147483647; /* ie 2**31-1 */
double temp = seed * a;
seed = (int) (temp - m * floor ( temp / m ));
return seed;
}

double rand_0to1()
{ return ( intrnd() / 2147483647.0 ); }

double random_number()
{
double r;
while ((r = rand_0to1() ) >= 1.0 ) ; /* ensure we don't return 1.0 */
return ( r );
}```

Any1 have idea how can i minimise the code? like 3-4 line instead all these???

Thanks Thanks

2. To get random numbers (truly random and uniformly distributed over a given range) you'll probably need more than 3-4 lines of code...

Use a seeding function that hashes the output of time like this:
Code:
```unsigned time_seed( time_t* T ){

time_t now = time( T );
unsigned char *p = ( unsigned char* )&now;
unsigned seed = 0;
size_t i;

for( i = 0; i < sizeof now; i++ )
seed = seed * ( UCHAR_MAX + 2U ) + p[i];

return seed;
}```
then uniformly deviate your random pool over your range like this:
Code:
```double uniformly_deviate( int seed ){

static const double R_MAX = RAND_MAX + 1.0;
return seed * ( 1.0 / ( R_MAX ) );
}```
and write a function to obtain your random number like so:
Code:
```// For an int..
int get_rand( ){

static const double R_MAX = RAND_MAX + 1.0;
return static_cast<int>( uniformly_deviate( rand() ) * R_MAX );
}

// For a double...
double get_rand_double( double LOWER_BOUND, double UPPER_BOUND ){

return ( LOWER_BOUND + uniformly_deviate( rand() ) * ( UPPER_BOUND - LOWER_BOUND ) );
}```

3. Originally Posted by peacealida
Hi everybody

Any1 have idea how can i minimise the code? like 3-4 line instead all these???

Thanks Thanks
Here.

Code:
```
unsigned int RandomNumber(){
unsigned __int64 hash;
static unsiged int seed;

if(seed == 0) seed = 1;

hash = (seed * 0x77ed1ab3) &#37; 4294967291;
seed = hash; // ignore compiler warning

return seed;
}```

4. Originally Posted by peacealida
Any1 have idea how can i minimise the code? like 3-4 line instead all these???
Using the built-in rand and srand functions should cut it down a lot.

Seriously, why write your own random functions that are worse than the built-in ones? I mean I can understand writing your own mersenne twister to improve upon the built-in one, but this...

Try holding down the delete key for a bit

5. Code:
```int intrnd () {
double const a    = 16807;      /* ie 7**5 */
double const m    = 2147483647; /* ie 2**31-1 */
double temp = seed * a;  seed = (int) (temp - m * floor ( temp / m ));  return seed;}
double rand_0to1()
{ return ( intrnd() / 2147483647.0 ); }
double random_number() {  double r;  while ((r = rand_0to1() ) >= 1.0 ) ;  return ( r ); }```
Now that's several lines shorter

As iMalc and dudeomandude hints, your random function is no better then the built in ones, and you could reduce it by several lines by using standard random numbers. Or you can write your own function that is better - but that would probably take a few more lines [at least if you don't want to mess up the formatting like I did - C is not a language where lines actually matter much - it's more what's on the lines that produce code that matters]

--
Mats

6. Code:
```unsigned int RandomNumber(){
unsigned __int64 hash;
static unsiged int seed;

if(seed == 0) seed = 1;

hash = (seed * 0x77ed1ab3) &#37; 4294967291;
seed = hash; // ignore compiler warning

return seed;
}```
Compiler warnings exist for good reason . . .
Code:
`   seed = (unsigned)hash;`
Besides the shortcomings already mentioned with your code, it's not very portable. I think __int64 is MS-specific.

For a quick and easy way to generate random numbers: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
For more detail about random numbers with better distribution: http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx

7. Hi
Sorry had few attempt, buy code doesn't seems to work after simplify

Any1 can help
thanks

8. Originally Posted by peacealida
Hi
Sorry had few attempt, buy code doesn't seems to work after simplify

Any1 can help
thanks
What code, and in what way does it not work?

--
Mats

9. Code:
```int intrnd () {
double const a    = 16807;      /* ie 7**5 */
double const m    = 2147483647; /* ie 2**31-1 */
double temp = seed * a;  seed = (int) (temp - m * floor ( temp / m ));  return seed;}
double rand_0to1()
{ return ( intrnd() / 2147483647.0 ); }
double random_number() {  double r;  while ((r = rand_0to1() ) >= 1.0 ) ;  return ( r ); }```

I have tried these code, but somehow occur 23 error

Thanks for the follow up and reply

10. I have tried these code, but somehow occur 23 error
What are the errors? At a glance, it is the same as your original example, except that matsp was trying to prove that reducing the number of lines of code may not be all that important.

11. Sorry It is working i had a messsssssssssssssssssssssssssssssssssssssssssssss

Popular pages Recent additions