@Random misbehaviour

The documentation says it all:“Generates a random number between 0 and 1, inclusive.”

Inclusive means you’ll have a hard time getting a random integer out of it.

@Integer(@Random*n) gives a random integer from 0 to n-1, but sometimes you’ll get a “n” (much less often than any other value of course).

So you can’t count it as one of the random target numbers, but if you did not consider this 0 case in your formula, sometimes something weird will happen.

Oh yes, you can also use @Round instead of @Integer to get an integer from 0 to n, but then 0’s and n’s occur twice less than other values – not really what you expect from a “random” function.

Every other random number generator implementation I know of (including LS) returns a number from 0 to 1 exclusive. There’s no wonder why.

Lotus choosed to do it another way at the time @Random was added in early Notes versions.

AFAIK the most correct way to generate a random integer in R6 is to use:

@DoWhile(r=@Integer(@Random*n);r=n)

In R4/R5, you can resort to @Integer(@Random-@Power(10;-15)), which only gets it minorly wrong.

Lastly, I think IBM could and should correct this bad behaviour.

Making @Random generate a random number between 0 and 1, EXCLUSIVE, won’t break anything: I can’t imagine any application relying on @Random returning one discreet value, be it 1.

While using @Random like you’d use a random function in any programming language and getting hit by the “inclusive 1” corner case sounds MUCH more likely to happen.

Comments?

Subject: @Integer(@Abs((@Random * n) - (1/(n * pad))))…

…where pad is any integer greater than 1. Add 1 and use n = x -1 for natural numbers 1 to x inclusive.

Subject: How did you come up with this? :slight_smile:

Hello Stan, thanks for your comment.

I don’t understand how you came up with this formula, but it looks very wrong.

It has 2 obvious defects:

  1. Chances to get a number won’t be equally distributed.

    Say you want to get a number from 0 to 3 and use a “pad” of 2.

    So your number is @Integer(@Abs(@Random*4 - 1/5)).

    (@Random*4 - 1/5) will range from -0.5 to 3.5.

    From -0.5 to 1 you get a 0, from 1 to 2 you get 1, from 2 to 3 you get 2 and from 3 to 3.5 you get 3.

    This is 3/8 of 0s, 2/8 of 1s, 2/8 of 2s and 1/8 of 3s.

    Quite not an equal distribution.

  2. You may still get n’s

    Say this time you want a number between 0 and 999999 and chose 10000 as your “pad” value.

    Your formula becomes @Integer(@Abs((@Random*1000000) - 1/10000000000))

    Since order of mangitude of the two operands of the substraction is so different, and a number in formula language holds “only” 16 digits in its mantissa, your formula really calculates to @Integer(@Abs((@Random*1000000))… and you still get n’s.

Too make it shorter:

For low n’s and pad the distribution is significantly unequal.

For high n’s and pad (n^2 * pad > 10^15) you may get a ‘n’, which is out of scope.

Yes, it will work for most common uses, but it’s hazardous and falky.

Let’s get it rationnal.

@Random is supposed to return a random discrete value between 0 and 1 included.

In this binary world, what it does actually return is a multiple of 10^-15, or 0.000000000000001.

And that’s (10^15 + 1) possible values.

We need to eliminate the corner case of ‘1’.

We can do that by substracting some small ‘µ’ value (which has to be as small as possible to avoid the distribution shifing problem).

Luckily, this will have a negligible impact at the other end of the distribution, since 0-µ will get zeroed by @Integer anyway (which mathematically speaking is yet another buggy function btw – the LS equivalent to @Integer is Fix(), while Int() really returns the integer part of a number – see Designer help).

So @Integer((@Random-@Power(10;-15))*n) is the most correct and reliable formula (in a strictly arithmetic way, excluding the use of an R6 iterative function as suggested in my previous post).

Subject: RE: How did you come up with this? :slight_smile:

I assumed anyone taking even a half-a$$ed shot at this would assume a reasonable value for “pad”. I suppose I should have specified “sufficiently large”. Using a sufficiently large value for pad, my formula is at least as random, inclusive and evenly distributed as either of yours.

By the way, no programmatic random number generator is truly random – and never will be unless it relies on collapsing nondeterministic values from a quantum probability wave to a real state.

Subject: RE: How did you come up with this? :slight_smile:

Doesn’t looks like you’ve even half read my post.Nevermind.

Subject: @Random misbehaviour

While I buy into the mathematics you’ve presented, I think part of me wonders why Lotus should be concerned. While Damien’s rewrite would probably make altering this behavior relatively easy, I wonder how many people using @random in formula language are really concerned with a mathematically proper distribution of outcomes. One would imagine that the more serious objective would necessitate the more serious tool.

Subject: RE: @Random misbehaviour

Hi Nathan.

Who’s this Damien you’re talking about?

Yeah, right, that’s not such a big deal.

Anyway, my grips against @Random are:

  1. It does not behave as usual random number generators do.

    If you did not mind it, your code will be buggy.

    And I’m talking about sneaky bugs here, the kind which will only break your code on a few runs, once in a while.

  2. This “new” behaviour is less practical.

    You have to work around the corner case where @Random=1.

Both lead to being more error prone.

On the other hand, making @Random compatible with other implementations:

  1. won’t break anything

  2. will eliminate the need for special handling of the @Random = 1 corner case

  3. will result in less bugs and cleaned code

So it looks like the right thing to do. :slight_smile:

About the use of formula (vs LS or JS), it was my only choice here since I use it in a toolbar button (only formulas are allowed here), which serves as a password generator.

Here is the code:

ascii_a := 97; ascii_0 := 48;

@While( @Length(pw) < 8;

@DoWhile (r := @Integer(@Random*36); r=36);

pw := pw + @Char (@If (r<26; r+ascii_a; r-26+ascii_0))

);

@Command([EditInsertText];pw)

Okay, I won’t be arguing any more on this topic, as I’ve already done so far too much and everyone who read all this probably thinks I’m just a short-minded s*cker. :slight_smile: (No I’m not!)

Subject: RE: @Random misbehaviour

On reconsideration, the best approach to generate integers between 0 and (n - 1) would be:

@Modulo(@Integer(n * @Random); n)

That will generate precisely one extra zero value in the case where @Random produces a 1 – and that would occur one time in the entire range of possible return values of @Random. That’s as close to never as it gets without actually being never, and n would need to be a HUGE number before the added zero instance becomes statistically significant.