32

I want to generate random numbers between (0,1). I am trying the following:

double r2()
{
 return((rand() % 10000) / 10000.0);
}
int SA()
{
 double u;
 u = r2();
}

But it doesn't generate the expected result. How can I fix it?

BrunoLM
101k86 gold badges311 silver badges464 bronze badges
asked Jun 2, 2011 at 18:04
10
  • 7
    define "not correct results" you may get a better response. Commented Jun 2, 2011 at 18:05
  • 4
    and btw. SA() does not return anything. Commented Jun 2, 2011 at 18:07
  • i didn't give all the code..this is the part where something is going wrong. Commented Jun 2, 2011 at 18:08
  • 1
    Your code works fine for me (after making SA return something). Commented Jun 2, 2011 at 18:09
  • How are you checking that u is not a "correct result"? Commented Jun 2, 2011 at 18:13

6 Answers 6

42

In your version rand() % 10000 will yield an integer between 0 and 9999. Since RAND_MAX may be as little as 32767, and since this is not exactly divisible by 10000 and not large relative to 10000, there will be significant bias in the 'randomness' of the result, moreover, the maximum value will be 0.9999, not 1.0, and you have unnecessarily restricted your values to four decimal places.

It is simple arithmetic, a random number divided by the maximum possible random number will yield a number from 0 to 1 inclusive, while utilising the full resolution and distribution of the RNG

double r2()
{
 return (double)rand() / (double)RAND_MAX ;
}

Use (double)rand() / (double)((unsigned)RAND_MAX + 1) if exclusion of 1.0 was intentional.

answered Jun 2, 2011 at 19:43
Sign up to request clarification or add additional context in comments.

1 Comment

In theory, there are some issues with the naive formula e.g., Uniform random floats: How to generate a double-precision floating-point number in [0, 1] uniformly at random given a uniform random source of bits (I don't know whether it matters in practice)
7

Here's a general procedure for producing a random number in a specified range:

int randInRange(int min, int max)
{
 return min + (int) (rand() / (double) (RAND_MAX + 1) * (max - min + 1));
}

Depending on the PRNG algorithm being used, the % operator may result in a very non-random sequence of numbers.

answered Jun 2, 2011 at 18:59

6 Comments

If the range is 0 to 1 as in the question, this function will return zero every time. It should perhaps return a double. What if a non-integer range were required? Either way this is somewhat over specified for the original requirement.
@Clifford How will this return 0 every time? (max-min+1) is 2 which means the rand() bit will return a number [0-2) which when truncated with (int) should roll 0 or 1 with equal probability.
It took three years for anyone to notice, and it even got a vote! I must have been confused by all the parentheses. However the original question implies a requirement for a double in the range 0 to 1, and this returns an int. Also RAND_MAX + 1 may overflow.
If this function is desired then it can be implemented using Clifford's function: return min+(int)(r2()*(max-min+1));
RAND_MAX + 1 can easily be INT_MIN or UB. Suggest (RAND_MAX/2 + 1)*2.0
|
1

It seems to me you have not called srand first. Usage example here.

answered Jun 2, 2011 at 18:11

1 Comment

That may not matter, and in some cases is undesirable (if you want repeatable testing for example), moreover this is obviously only a fragment, and is not teh cause of his problem.
0
double r2()
{
 return (rand() % 10001) / 10000.0;
}
answered Jul 4, 2019 at 13:12

Comments

0

Set the seed using srand(). Also, you're not specifying the max value in rand(), so it's using RAND_MAX. I'm not sure if it's actually 10000... why not just specify it. Although, we don't know what your "expected results" are. It's a random number generator. What are you expecting, and what are you seeing?

As noted in another comment, SA() isn't returning anything explicitly.

http://pubs.opengroup.org/onlinepubs/009695399/functions/rand.html http://www.thinkage.ca/english/gcos/expl/c/lib/rand.html

From Generating random number between [-1, 1] in C? ((float)rand())/RAND_MAX returns a floating-point number in [0,1]

Georg Plaz
6,0185 gold badges43 silver badges66 bronze badges
answered Jun 2, 2011 at 18:23

6 Comments

i used srand(time(NULL)); and the result i get is for example 7.8. i want numbers between (0,1).
Yes... we know you want (0,1), it's in the question/description twice. How were we supposed to know that you used srand()? What about specifying the max value to rand()? You also say you "obviously" used printf. How is that obvious to us if you only give us bits and pieces of your code? Help us help you.
how can i specify the max value to rand()? you don't have to be mean i used it without srand() and i got the same results.
Hmm, I thought you can specify an int value to pass in to rand(), my mistake. I would divide/mod by RAND_MAX, however. stackoverflow.com/questions/1557208/…
thank you very much and sorry about the mean....but i got a lot of angry answers!i found what i did wrong! thank you very very much for your time solving my problem! :)
|
-2

Have you tried with: replacing ((rand() % 10000) / 10000.0), with:(rand() % 2). This worked for me!

So you could do something like this:

double r2()
{
 srand(time(NULL));
 return(rand() % 2);
}
answered Aug 9, 2017 at 18:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.