Skip to main content
4-Participant
November 24, 2023
Solved

Generate n samples of size m from uniform (0,1) using MATHCAD PRIME

  • November 24, 2023
  • 2 replies
  • 2802 views

I am trying to generate n samples of size m from the uniform (0,1). I am using runif(1,0,1) in the loop. Please see the attached file. Thanks

Best answer by Cornel

Like this?

Cornel_0-1700828985305.png

or like this?

Cornel_0-1700829071167.png

 

2 replies

Cornel19-TanzaniteAnswer
19-Tanzanite
November 24, 2023

Like this?

Cornel_0-1700828985305.png

or like this?

Cornel_0-1700829071167.png

 

25-Diamond I
November 24, 2023

@Cornel 

You are creating n*m=1000 times a vector with  n=10 elements (it should be a vector with m=100 elements) but your programs only return the last one created. That quite inefficient 😉

25-Diamond I
November 24, 2023

@KN_9565343 wrote:

I am trying to generate n samples of size m from the uniform (0,1). I am using runif(1,0,1) in the loop. Please see the attached file. Thanks


So I interpret what yo write that way that at the end you would like a nested n x 1 vector where each element is a m x 1 vector of random numbers.

 

So must note that "runif"  always creates a matrix/vector. The command runif(1,0,1) creates a 1 x 1 matrix, wbich is probably not what you want.

Your program actually creates the n * m random values, but every new run in the k-loop overwrites the values created in the i-loop before. Furthermore a program (unless you explicitly use the "return" statement) always returns the last value it calculated and in our case thats the last value b[m in the last run of your loops and thats the reason you juts get one single 1x1 matrix with just one number as your output.

 

If you really want to calculate each of the n*m values singly you should use the "rnd" conmmand and not "runif".
The most "logical" way to do it with two nested loops would be this

Werner_E_0-1700831462380.png

but Prime refuses the usage of the double vector indices when creating b[i]k. So we have to resort to using am temporary variable

Werner_E_1-1700831532789.png

 

BUT ....

as you already know about the runif command and its ability to create a vector, why not use it to create each m x 1 vector in one go and just use the k-loop to create n of these vectors:

Werner_E_2-1700831644876.png

I would prefer this way (using a loop), but you could also use a range variable to do the job instead of a for-loop

Werner_E_3-1700831765671.png

 

Prime 9 file attached

25-Diamond I
November 24, 2023

One additional interpretation - maybe you would prefer a m x n matrix, where each column represents a m x 1 vector of random numbers

Werner_E_4-1700832367032.png

or even shorter using runif:

Werner_E_5-1700832457486.png

 

You may also consider using a more general function which you could multiple times in the worksheet to create different sets of numbers

Werner_E_6-1700832835606.png

 

Werner_E_7-1700832847997.png

You could also make the number limits (o and 1 in your example) as additional function arguments

Werner_E_8-1700832997255.png

Werner_E_9-1700833165964.png

 

Modified P9 sheet attached

4-Participant
November 26, 2023

Thank you for the prompt solution, Werner. Highly appreciated.  However, the first suggestion does not work. Please see the attached code. It gives an error saying the tmp variable is not defined.