MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: easy question about random numbers


Thanks for helping me, Bobby.

I have been trying the differents methods for creating random numbers and
I think the faster are these two:

-------------- First method
li2 = {1/6, 1/6, 1/6, 1/6, 9/60, 11/60};
li4 = Rationalize[li2 LCM @@ Denominator[Rationalize[li2]]];
a = Flatten[Table[Table[i, {li4[[i]]}], {i, Length[li4]}]];

And now it's necessary to obtain by hand the length of "a":
Length[a]
Out[]:=60

Finally, this is the  function:
(a[[Random[Integer, {1, 60}]]] & )[]

-------------- Second method
setal[p_] := Block[{n = Length[p], a, b, c, i, j, tol}, a = Range[n]; b = n*p - 1; c = Table[1, {n}]; tol = 2*n*$MachineEpsilon; 
     While[{i, j} = Ordering[b][[{1, -1}]]; b[[j]] > b[[i]] + tol, a[[i]] = j; c[[i]] = 1 + b[[i]]; b[[j]] += b[[i]]; 
       b[[i]] = 0]; {n, a, N[c]}]; 
p = (#1/Plus @@ #1 & )[Table[Log[Random[]], {6}]]; {n, a, c} = setal[p];

And this is the function:
(If[Random[] > c[[#1]], a[[#1]], #1] & )[Random[Integer, {1, n}]]

--------------------------------------

But I'm sorry to say that I'm not able of understanding the second
method (the "alias" method).
What do you think about the first method?


Pedrito



  • Prev by Date: Re: easy question about random numbers
  • Next by Date: Re: easy question about random numbers
  • Previous by thread: Re: easy question about random numbers
  • Next by thread: Re: easy question about random numbers