Generating random numbers with Mathematica

In summary: It is also worth mentioning that for certain problems (such as this one) it may be more efficient to use a loop than a for-loop.
  • #1
maani
10
0
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!
 
Physics news on Phys.org
  • #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.
 
  • #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?
 

Attachments

  • code.nb
    7.7 KB · Views: 601
  • #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:
  • #5
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.
 

FAQ: Generating random numbers with Mathematica

What is Mathematica?

Mathematica is a powerful software program used for various scientific and mathematical calculations, data analysis, and visualization. It is commonly used by scientists, engineers, and researchers in various fields.

How does Mathematica generate random numbers?

Mathematica uses a pseudorandom number generator (PRNG) algorithm to generate random numbers. This algorithm uses a mathematical formula to produce a sequence of numbers that appear random, but are actually deterministic.

What is the range of random numbers that can be generated in Mathematica?

The range of random numbers that can be generated in Mathematica is from 0 to 1. This is the default range, but it can be adjusted using the RandomReal function by specifying a minimum and maximum value.

Can Mathematica generate random numbers from a specific distribution?

Yes, Mathematica can generate random numbers from a variety of distributions, such as uniform, normal, exponential, and more. These can be accessed using the RandomVariate function and specifying the desired distribution.

How can I ensure that the random numbers generated in Mathematica are truly random?

To ensure that the random numbers generated in Mathematica are truly random, it is important to set a seed for the PRNG. This can be done using the SeedRandom function, which takes in a specific integer as the seed. This will ensure that the same sequence of random numbers is not generated each time the code is run.

Similar threads

Replies
1
Views
2K
Replies
1
Views
1K
Replies
1
Views
3K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
6
Views
2K
Replies
4
Views
1K
Back
Top