3

I would like to generate N random points whose product is some value 1.

I did it in MATALB this way:

N_=10;
x1_=rand(1, N_);
p_=prod(x1_);
x_=x1_;
x_(end)=x1_(end)/p_;
prod(x_);

Is there a simple (other way)? And What about when P ~= 1?

Thanks.

npisinp
  • 370
  • 1
  • 11

4 Answers4

4

One way to do this is to use the fact that log(1) = 0, and a*b = exp(log(a+b)).

x = rand(N,1);
norm_log_x = log(x)-mean(log(x));
norm_x = exp(norm_log_x);
norm_x =

    2.6058
    2.7166
    0.6396
    1.3958
    1.0874
    1.5147
    0.7660
    0.1977
    1.2618
    0.5028

prod(norm_x)
ans =
    1.0000

To get a value other 1, you can do:

P = 7;
norm_log_x = log(x)-mean(log(x))+log(P)/length(x);
norm_x = exp(norm_log_x);
prod(norm_x)    
ans =
    7.0000
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
1

In your case, the last number has a different distribution. Assuming you want the same distribution for all numbers:

N_=10;
x1_=rand(1, N_);
p_=prod(x1_);
x_=x1_./(p_.^(1/N_))
Daniel
  • 36,610
  • 3
  • 36
  • 69
1

Lognormal distribution has property that product is again lognormal distributed. This also mean, that when you can make last "fix" and it will be impossible to find number that was used for this "distribution trimming".

Luka Rahne
  • 10,336
  • 3
  • 34
  • 56
0

I don't have access to MATLAB right now but couldn't you just do..

n      = 10;
x_sum  = 1;
x_rand = rand(x_sum, n);
x_rand = x_rand ./ sum(x_rand);

This way gives a random list and you could change x_sum if you didn't want the sum to be 1. If you want a uniform distribution or some other uniform distribution you can just replace rand() with a different built in function.

edit: I would run this quick to double check my code is right.

sum(x_rand);

Daniel me
  • 171
  • 1
  • 13