• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

create random numbers

Hi there!

I need to create several random numbers between 0 and 1. I have tried using rand() and I am able to generate random numbers but they dont change significantly (ie. .4456, .4489, etc)


Any ideas?


Thanks!
0
Indietrasher
Asked:
Indietrasher
1 Solution
 
IndietrasherAuthor Commented:
OK!


I found it!

I used:

srand( time( NULL) );

number = ( (float)rand() / (float)(RAND_MAX+1));

srand must be called only once in he program and then it woll generate a random sequence of numbers...


Thanks anyway!
0
 
stefan73Commented:
Hi Indietrasher,
Don't use rand(). It's one of the worst random generators around. If you're on Unix, check drand48():

#include <stdlib.h>
double drand48(void);

Results are usually much better - many rand() implementations only return 15 bits as result, and the internal seed state is better for the ...rand48 family of functions.

Cheers!

Stefan
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now