Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Generating random numbers with Mathematica

  1. Jul 10, 2012 #1
    Hi everyone,
    I am trying to generate 200 random numbers from an exponential distribution which have to add to one.
    I guess I need a loop where in each step I generate a random number from the exponential distribution and check the sum, if it is less than one I add the number to a list and if not I generate another number. So at the and I have a list with 200 numbers which add to one.
    The problem is I fail to implement this. Does anybody know an easier way to do this?

    I will be grateful for any suggestions!
  2. jcsd
  3. Jul 10, 2012 #2
    Purists will rightfully say "but when you have constrained the total to be a sertain number the distribution is no longer random." Imagine you have chosen 190 of the 200 numbers, the total is almost exactly 1 and you are left with choosing 10 more numbers, all almost exactly zero. The distribution of the result will be skewed and not really exponential.

    What is the lambda parameter you are using for your exponential distribution?
    Does the total have to be EXACTLY 1.0?
    Do you only need to do this one time and speed is not a concern or must this be done quickly?

    Perhaps with that a line or two of code can be written to give you a solution.
  4. Jul 11, 2012 #3
    Hi Bill,

    thanks for your answer! To your questions:

    2.The total of the numbers has to be exactly 1.0!I know strictly speaking these numbers are not random but I just don't know how to call them.

    3. I have to do this more than once, so it has to be done relatively quickly.

    1. As for the parameter λ I am not so sure what I should take. If I take λ too small (1-10) it will be very difficult, almost impossible to generate 200 random numbers with total 1. I choose exponential distribution because I need numbers from a wide range. But if I take λ too big (200) I cannot achieve that.
    I also thought of changing λ in every step (see attached file). However it takes too long.

    Do you have another idea?

    Attached Files:

    • code.nb
      File size:
      7.7 KB
  5. Jul 11, 2012 #4
    To have 200 numbers with an exponential distribution sum to 1 your lambda must be almost exactly 200.

    To get the total to be exact I offer the following

    v = RandomVariate[ExponentialDistribution[200], 200];
    v = v/Total[v]

    That seems to commit the least sin against an exponential distribution while still obtaining your total, but this isn't my field.

    If an exponential distribution does not satisfy your requirements then I suggest thinking carefully about your problem and determining exactly what distribution models your problem correctly
    Last edited: Jul 11, 2012
  6. Jul 12, 2012 #5


    User Avatar
    Science Advisor

    Hey maani and welcome to the forums.

    It might be helpful if you gave some context to your problem so that the readers can assess if the method you are using may be problematic.

    Bill's idea to standardize the sum is a good one, but depending on what you are trying to do (as he has already pointed out), it may give misleading or simply incorrect analyses for your problem at hand.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook