-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?

  • 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. – MrHappyAsthma Jul 17 '13 at 13:55
  • 4
    Use [`std::uniform_real_distribution<>`](http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution). –  Jul 17 '13 at 13:55
  • 2
    Which are you using, C or C++? – James M Jul 17 '13 at 13:57
  • divide by ten for range [ 0 0.1] – hetepeperfan Jul 17 '13 at 13:57
  • @rightfold If he wants `[0,1)` then [std::generate_canonical](http://en.cppreference.com/w/cpp/numeric/random/generate_canonical) is better. – Rapptz Jul 17 '13 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](http://icecube.wisc.edu/~dglo/c_class/promo_conv.html)). – Bernhard Barker Jul 17 '13 at 14:00
  • there in no differences between c or c++ for me – hosein hadian Jul 17 '13 at 14:06
  • Is `0.0002568968931` also fine? Meaning the number must be < `0.0010`, or do you want a number in the form `0.000x00` where x is some digit? – Bernhard Barker Jul 17 '13 at 14:10
  • @Dukeling: Actually OP's final cast is not a no-op. It throws away excess precision leftover from the division. I don't claim OP used it this was intentionally, but it's worth noting. – R.. GitHub STOP HELPING ICE Jul 17 '13 at 14:10
  • @R.. Can you elaborate? As far as I'm aware, `(double)rand()/RAND_MAX` returns a `double`, thus `(double)((double)rand()/RAND_MAX)` casts a `double` to a `double`, thus doesn't do anything. Or by "final" do you mean "right-most"? In which case, to my knowledge, `RAND_MAX` will get promoted to `double` before the multiplication without an explicit cast, thus the cast isn't necessary. – Bernhard Barker Jul 17 '13 at 14:12
  • @Dukeling: I mean the leftmost. See C99 6.3.1.8 *The values of floating operands and of the results of floating expressions may be represented in greater precision and range than that required by the type; the types are not changed thereby.* ... – R.. GitHub STOP HELPING ICE Jul 17 '13 at 14:16
  • ... and 6.3.1.5 *When a double is demoted to float, a long double is demoted to double or float, or a value being represented in greater precision and range than required by its semantic type (see 6.3.1.8) is explicitly converted (including to its own type), if the value being converted can be represented exactly in the new type, it is unchanged. If the value being converted is in the range of values that can be represented but cannot be represented exactly, the result is either the nearest higher or nearest lower representable value, chosen in an implementation-defined manner.* – R.. GitHub STOP HELPING ICE Jul 17 '13 at 14:17

2 Answers2

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

sedavidw
  • 11,116
  • 13
  • 61
  • 95
  • could you tell me more about inverse transform sampling and the way of implementing that – hosein hadian Jul 17 '13 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 (http://en.wikipedia.org/wiki/Box-Muller_transform <- See Basic Form) – sedavidw Jul 17 '13 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. – hosein hadian Jul 17 '13 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 '13 at 15:19
  • no x is a number between 2 and 10000 – hosein hadian Jul 17 '13 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;
Rayee Roded
  • 2,440
  • 1
  • 20
  • 21