RND()

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Not supported in Run BASIC

Description

This function returns a pseudo random number, 0<number<1.
Syntax requires any_expression, but given number or expression is ignored (so passing 0 or 1 is common).
To always get the same set of random numbers, you can seed the generator with the RANDOMIZE command.

Syntax

  1. RND(0)
  2. RND(any_expression)

Hints

Pseudo random number generator used in JB probably good enough for 99% of cases. But if you care for your numbers appear to be "random", you should read RANDOMIZE Hints section.
Pseudo random number generator supposed to have uniform distribution, however JB one is slightly off. How much off?
As an example, if we count RND() >0.5, there will be systematic bias around 0.5% (normally it should be about 0). Any other counts will likely be off as well.
It's probably OK for most games, but for numerical simulation you should bear that in mind - and may be even use another generator.
Do random numbers repeat?
There was conducted an experiment looking for repeating values in more then 2^32 random numbers. The sequence did not appear to be repeating (from initial 5 numbers, there were no more then two numbers repeated in a row).

Example

For i = 1 To 10
    Print RND(0)*2
Next
Print "---"
For i = 1 To 10
    Print INT(RND(0)*10) + 1
Next
End

RND bias and "unbiased coin" function

'demonstrating rnd() bias in flipping the coin.
'Also, 'unbiased coin' function
randomize 0.5
N=100000
print "JB rnd() function (biased)"
randomize 0.5
for k=1 to 10
    p1=0
    for i=1 to N
        p1=p1+coin()
    next
    p2=N-p1
    print p1, p2, (p1-p2)/N*100;"%"
next

print "Unbiased coin"
randomize 0.5
for k=1 to 10
    p1=0
    for i=1 to N
        p1=p1+unBiasedCoin()
    next
    p2=N-p1
    print p1, p2, (p1-p2)/N*100;"%"
next

function coin( )
    coin = rnd(1)>0.5
end function

function unBiasedCoin( )
'The actual unbiasing should be done by generating two numbers at a time
'from randN and only returning a 1 or 0 if they are different.
'As long as you always return the first number or always return the second number,
'the probabilities discussed above should take over the biased probability of randN.
[again]
    n1 = rnd(1)>0.5
    n2 = rnd(1)>0.5
    if n1=n2 then [again]
    unBiasedCoin = n1
end function

Useful Procedures

Random Number in Range

print "0..5",  "1..5.9999", "1..5", "1..6"
print "------------------------------------------------"
for i = 1 to 20
    print random(6),
    print RndRange(1,6),
    print RndIntRange(1,6),
    print RndIntRangeIncl(1,6)
next

'-------------------------------------------------------

'function returns a random INTEGER in range [0, n): 0<=random(n)<n
'Like in some C compilers
function random(n)
    random = int(rnd(1)*n)
end function

'returns a random number between lower and upper bounds:
'LowerBound<= RndRange(LowerBound, UpperBound) <UpperBound
Function RndRange(LowerBound, UpperBound)
    RndRange = Rnd(0) * (UpperBound - LowerBound) + LowerBound
End Function

'returns a random INTEGER between lower and upper bounds:
'LowerBound<= MyRnd(LowerBound, UpperBound) <UpperBound
Function RndIntRange(LowerBound, UpperBound)
    RndIntRange = Int(Rnd(0) * (UpperBound - LowerBound)) + LowerBound
End Function

'returns a random INTEGER between lower and upper bounds, INCLUSIVE:
'LowerBound<= MyRnd(LowerBound, UpperBound) <=UpperBound
'(handy for say dice simulation: RndIntRangeIncl(1,6) )
Function RndIntRangeIncl(LowerBound, UpperBound)
    RndIntRangeIncl = Int(Rnd(0) * (UpperBound - LowerBound+1)) + LowerBound
End Function