By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

RAND between 0 and 1

P: n/a
Hi all,

I think this has been posted many times.. but I tried several recipes
for obtaining a random number between [0,1], but I can't make them work.

So, does anyone know how to generate random number x, for 0<= x <=1??

Thanks,

FBM

That's what I got.......

float rndm = (float) (rand() / RAND_MAX);

and I tried also

float rndm = (float) (rand() % 2);

with no success...
Nov 15 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Fernando Barsoba wrote:
I think this has been posted many times.. but I tried several recipes
for obtaining a random number between [0,1], but I can't make them work.

So, does anyone know how to generate random number x, for 0<= x <=1??

Question 13.16 in the FAQ: http://www.eskimo.com/~scs/C-faq/q13.16.html.
Also check out the questions in the vicinity.

S.
Nov 15 '05 #2

P: n/a
On Thu, 29 Sep 2005 03:51:13 GMT, Fernando Barsoba
<fb******@verizon.net> wrote in comp.lang.c:
Hi all,

I think this has been posted many times.. but I tried several recipes
for obtaining a random number between [0,1], but I can't make them work.
Define what you mean by now working.
So, does anyone know how to generate random number x, for 0<= x <=1??

Thanks,

FBM

That's what I got.......

float rndm = (float) (rand() / RAND_MAX);
Aha! Integer division! RAND_MAX is a macro that evaluates to a
numeric literal of type int. rand() returns a value that is of type
int. The result of dividing an int by an int is an int quotient, with
any remainder discarded. Casting the result to a float after the
integer division/truncation does not bring the fractional part back.
and I tried also

float rndm = (float) (rand() % 2);

with no success...


In the future, don't just say "doesn't work". Describe the results
you are getting. It makes no difference in this case, but very often
it does when someone is trying to locate the cause of your problem.

In any case, you need to cast one or the other of the operands of the
division to a float. This will cause promotion of the other operand
to a float. Then a float division will be performed on the two float
values, yielding a float result. In any case, you do not need the
cast on the assignment to a float, that conversion is automatic and
the cast operator does absolutely nothing.

float rndm = rand()/(float)RAND_MAX;

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #3

P: n/a

"Fernando Barsoba" <fb******@verizon.net> wrote
float rndm = (float) (rand() / RAND_MAX);

This is the way to do it. However you need to cast to a float first,
otherwise you will get an integer division.
Incidentally it is traditional to divide by RAND_MAX plus one, because a lot
of algorithms don't work as nicely when a random number on the interval 0-1
is exactly unity.
Nov 15 '05 #4

P: n/a
Fernando Barsoba wrote:
Hi all,

I think this has been posted many times.. but I tried several recipes
for obtaining a random number between [0,1], but I can't make them work.

So, does anyone know how to generate random number x, for 0<= x <=1??


You could check the FAQ before posting. Or I could spoonfeed you:

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

inline double closed_interval_rand(double x0, double x1)
{
return x0 + (x1 - x0) * rand() / ((double) RAND_MAX);
}

int main(void)
{
int pass;
srand(time(0));
for (pass = 0; pass < 10; pass++)
printf("%d: %g\n", pass, closed_interval_rand(0, 1));
return 0;
}

[output for one invocation]
0: 0.589319
1: 0.108868
2: 0.13723
3: 0.575189
4: 0.444024
5: 0.657485
6: 0.83961
7: 0.00479315
8: 0.849472
9: 0.740225

Nov 15 '05 #5

P: n/a
Is there any reason to avoid using this:

srand(time(0));

float random_number = rand();

Nov 15 '05 #6

P: n/a
Why is it better:

rand() / (RAND_MAX / N + 1)

than:

srand(time(0));
rand() % N;

Nov 15 '05 #7

P: n/a
Thanks to all for your postings. I meant to say that the result was
0.0.. but now I solved it thanks to your comments.
float rndm = (rand() / (float)RAND_MAX);

The srand(time(null)) I used it to obtain a non-predictable random
number generator (if i say it correctly..)

cheers,

FBM
Fernando Barsoba wrote: Hi all,

I think this has been posted many times.. but I tried several recipes
for obtaining a random number between [0,1], but I can't make them work.

So, does anyone know how to generate random number x, for 0<= x <=1??

Thanks,

FBM

That's what I got.......

float rndm = (float) (rand() / RAND_MAX);

and I tried also

float rndm = (float) (rand() % 2);

with no success...

Nov 15 '05 #8

P: n/a
"Gaijinco" <ga******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Why is it better:

rand() / (RAND_MAX / N + 1)

than:

srand(time(0));
rand() % N;


The problem with random integers in a desired interval is that when you
start doing tricks like the above the distribution ceases to be uniform.
Here's an example revealing this problem:

#include <stdio.h>

#define MY_RAND_MAX 15 // my RAND_MAX for myrand()
#define N 10 // the N in myrand()%N
#define M 1000 // iterations count

// My fake rand() function,
// mimicking uniform distribution.
// THIS IS A BAD random generator,
// BUT A GOOD means to show the problem
// of the rand()%N approach to generate
// uniformly distributed numbers in the range
// smaller than [0,RAND_MAX].
int myrand()
{
static int seed = 0;
int res = seed;
// generate numbers in the range [0,MY_RAND_MAX]
// like so: 0,1,2,...,MY_RAND_MAX-1,MY_RAND_MAX,0,1,2,...
if (++seed > MY_RAND_MAX)
seed = 0;
return res;
}

int main()
{
int i;
int aCnt[N];

// zero up counters of each of the numbers
// that will be generated at random:
for (i=0; i<N; i++)
aCnt[i] = 0;

// generate numbers at random and count
// how many time each of them was generated:
for (i=0; i<M; i++)
aCnt[myrand()%N]++;

// print out the statistics:
printf ("Statistics:\n"
"+--------+-------------------+\n"
"| Number | Times Encountered |\n"
"+--------+-------------------+\n");
for (i=0; i<N; i++)
printf ("| %-6d | %-17d |\n", i, aCnt[i]);
printf ("+--------+-------------------+\n");

return 0;
}
Alex
Nov 15 '05 #9

P: n/a
Fernando Barsoba <fb******@verizon.net> writes:
Thanks to all for your postings. I meant to say that the result was
0.0.. but now I solved it thanks to your comments.
>> float rndm = (rand() / (float)RAND_MAX);


The srand(time(null)) I used it to obtain a non-predictable random
number generator (if i say it correctly..)


Please don't top-post; your response goes below any quoted text, which
should be trimmed down to what's relevant.

I think you mean srand(time(NULL)). This isn't bad, but it does
assume some things that aren't guaranteed by the standard. The time()
function returns a result of type time_t, which is an arithmetic type
capable of representing times. It could legally be a floating-point
value in the range 0.0..1.0, which would result in always passing 0 to
srand(), which would always give you the same sequence of numbers. I
don't know of any implementations that behave this way, so that's
probably fairly safe. (srand() takes an argument of type unsigned
int, so the conversion from time_t isn't going to be a problem.)

Another problem is that the standard rand() function is often poorly
implemented. If the security of your system depends on high-quality
unpredictable random numbers (e.g., if you're doing cryptography), you
should find some system-specific random number generator. In
particular, using time() to seed the generator is good enough for some
applications, but could make it possible for an adversary to predict
the behavior of your program if he can guess what time it was started.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #10

P: n/a
<posted & mailed>

float r = ((float) rand()) / RAND_MAX;

.... rand() returns and integer that is equal to or less than RAND_MAX.
Dividing an integer by an integer larger than it yields 0, if the same, 1.

Fernando Barsoba wrote:
Hi all,

I think this has been posted many times.. but I tried several recipes
for obtaining a random number between [0,1], but I can't make them work.

So, does anyone know how to generate random number x, for 0<= x <=1??

Thanks,

FBM

That's what I got.......

float rndm = (float) (rand() / RAND_MAX);

and I tried also

float rndm = (float) (rand() % 2);

with no success...


--
Remove '.nospam' from e-mail address to reply by e-mail
Nov 15 '05 #11

P: n/a
Gaijinco wrote:
Why is it better:

rand() / (RAND_MAX / N + 1)

than:

srand(time(0));
rand() % N;


Its not. See: http://www.azillionmonkeys.com/qed/random.html

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Nov 15 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.