-1

I use this code to create random number between zero and 1.

randnumber = (double)((double)rand() / (double)RAND_MAX);

This line of code regularly give me numbers like 0.650434, 0.302340 and 0.906789 which are big numbers for me. The problem is I need smaller range number like these numbers: 0.000200, 0.000100, 0.000400, 0.000600 and so on. how can I modify this line of code to get my result?

12
  • 4
    Could you not just random from 1-100 and then divide each number by 10000? (Or whatever scale factor you need)? I don't see a need to do the division within the random function. Jul 17, 2013 at 13:55
  • 4
    Use std::uniform_real_distribution<>.
    – user142019
    Jul 17, 2013 at 13:55
  • 2
    Which are you using, C or C++?
    – James M
    Jul 17, 2013 at 13:57
  • divide by ten for range [ 0 0.1] Jul 17, 2013 at 13:57
  • 2
    You have casting overkill, consider just randnumber = (double)rand()/RAND_MAX (equivalent to randnumber = ((double)rand())/RAND_MAX due to operator precedence which is equivalent to what you've got due to variables promotion). Jul 17, 2013 at 14:00

2 Answers 2

3

Do you know what kind of probability distribution function you're looking for? If you still want uniform you can simply divide the [0,1) result by a large number and apply a shift so that it's centered around the value you want.

If you want something that's normally distributed you can use something like the inverse transform method. Again you'll need to center it and modify the standard deviation. There are similar transformations to other distributions as well

5
  • could you tell me more about inverse transform sampling and the way of implementing that Jul 17, 2013 at 14:13
  • In order to help you I need to know what kind of distribution you're trying to get to. If you're trying to get to a normal distribution the Box-Muller transform is easy to implement and not terribly slow (en.wikipedia.org/wiki/Box-Muller_transform <- See Basic Form)
    – sedavidw
    Jul 17, 2013 at 14:17
  • you know I have this probability function: probability =((x -2) / 10000 ) and want to create a random number by the rand() function between zero and 1. Then compare random number with probability. If the the random number is less than probability do some thing. the problem is the distribution of randnumber is not adaptive with the distribution of probability function. Jul 17, 2013 at 14:25
  • Is x a random variable that produces a value on the range [0,1)? If so what's your problem?
    – sedavidw
    Jul 17, 2013 at 15:19
  • no x is a number between 2 and 10000 Jul 17, 2013 at 15:39
1

This gives random number between 1 and 100

int randInt= (rand()%100)+1;

so divide it, to get your desired range

double yourRange=(double)randInt/100000;
1
  • 2
    It gives a non-uniform result distribution and also happens to have very poor entropy on some implementations. Jul 17, 2013 at 14:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.