# [latex3-commits] [l3svn] r6750 - Add random numbers to l3fp, allow 0-argument functions

Bruno Le Floch blflatex at gmail.com
Sat Dec 3 15:02:17 CET 2016

```On 12/03/2016 08:28 AM, Bruno Le Floch wrote:
> On 12/02/2016 08:44 AM, Chris A Rowley wrote:
>>
>> On 28 Nov 2016, at 20:04, David Carlisle <d.p.carlisle at gmail.com> wrote:
>>
>>>> \MP\ generates pseudo-random numbers with the additive scheme
>>>> recommended in Section 3.6 of {\sl The Art of Computer Programming};
>>
>> Not our problem, but I think,that should be     . . . .Programming, Volume 2}
>>
>> Chris
>>
>
> Thanks Chris, I'm trying to figure out what to document, so I looked at
> the code in pdftex.web, and the following paragraph gets me worried
>
> -----
>
> @ To produce a uniform random number in the range |0<=u<x| or |0>=u>x|
> or |0=u=x|, given a |scaled| value~|x|, we proceed as shown here.
>
> Note that the call of |take_frac| will produce the values 0 and~|x|
> with about half the probability that it will produce any other particular
> values between 0 and~|x|, because it rounds its answers.
>
> @p function unif_rand(@!x:integer):integer;
> var @!y:integer; {trial value}
> begin next_random; y:=take_frac(abs(x),randoms[j_random]);
> if y=abs(x) then unif_rand:=0
> else if x>0 then unif_rand:=y
> else unif_rand:=-y;
> end;
>
> -----
>
> Am I reading correctly that they take randoms[j_random], namely a 32 bit
> integer, and rescale it to whatever interval the user requests?  This
> doesn't give uniform results at all, especially when the output range is
> close to 32 bits.  I'd have to document this and try to think whether
> there is a way around that.
>
> Best,
>
> Bruno
>

Worse than I expected: randoms contains 28-bit integer, which means for
instance that \pdfuniformdeviate 1073741824 produces no odd integer.
Infinite loop:

\loop
\ifodd\pdfuniformdeviate1073741824\relax
\else
\repeat

Should we try to work around that?  One option is to systematically call
\pdfuniformdeviate with argument 2^28.

Best,

Bruno
```