RAN() misbehaving
#1
Description:
The ran-function returns a random number. If no argument is given, the number returned is in the range from 0 to 1; where only 0 is a possible value; 1 will never be returned. If an argument is supplied, the number returned will be in the range from 0 up to this argument, whereas this argument itself is not a possible return value.


OK, so RAN(7) is supposed to give me a random number between 0 and 6.9999. This is how the command has worked in every version of BASIC I've ever seen. It seems to be broken in yab.

Code:
~/projects/DailyHaiku/binder> touch ran.bas
~/projects/DailyHaiku/binder> echo "print ran(7)">ran.bas
~/projects/DailyHaiku/binder> cat ran.bas
print ran(7)
~/projects/DailyHaiku/binder> yab ran.bas
2.04283
~/projects/DailyHaiku/binder> yab ran.bas
2.04294
~/projects/DailyHaiku/binder> yab ran.bas
2.04299
~/projects/DailyHaiku/binder> yab ran.bas
2.04305
~/projects/DailyHaiku/binder> yab ran.bas
2.04305
~/projects/DailyHaiku/binder> yab ran.bas
2.0431
~/projects/DailyHaiku/binder> yab ran.bas
2.04316
~/projects/DailyHaiku/binder> yab ran.bas
2.04327
~/projects/DailyHaiku/binder> yab ran.bas
2.04332

Yes, those are technically random numbers. But nowhere near the range the command is supposed to give.

yab 1.7.5 running on Haiku hrev49371

Let's increase that seed number to 14

Code:
~/projects/DailyHaiku/binder> rm ran.bas
~/projects/DailyHaiku/binder> touch ran.bas
~/projects/DailyHaiku/binder> echo "print ran(14)">ran.bas
~/projects/DailyHaiku/binder> /bin/yab ran.bas
4.22032
~/projects/DailyHaiku/binder> /bin/yab ran.bas
4.22086
~/projects/DailyHaiku/binder> /bin/yab ran.bas
4.22097
~/projects/DailyHaiku/binder> /bin/yab ran.bas
4.22108
~/projects/DailyHaiku/binder> /bin/yab ran.bas
4.22119
~/projects/DailyHaiku/binder> /bin/yab ran.bas
4.2213
~/projects/DailyHaiku/binder> /bin/yab ran.bas
4.22141

The same thing happens with an old (1.7) version of yab, so the problem either is in the underlying OS, or I am not understanding the documentation.

RAN() without a seed number works as expected so as a workaround we can use (RAN()) * 7

In fact it is worse: if you try

for f = 1 to 10
print ran()
next

and run it a few times the first one always seems to start with 0.3, and only after that does it start to look random at all.
Reply
#2
(07-06-2015, 01:04 PM)clasqm Wrote: for f = 1 to 10
print ran()
next

I always use :
Code:
tmp=ran()
for f = 1 to 10
print ran()
next

and ignore the output of the first line. The rest seem to return a reasonable, sort-of random, number.

It seems to be a gcc issue.
Reply
#3
(07-06-2015, 01:43 PM)bbjimmy Wrote: I always use :
Code:
tmp=ran()
for f = 1 to 10
print ran()
next

Yes, I figured that out as well, but it does not fill me with confidence. Here are two more workarounds. 99% of the time, I convert the RAN output to integer anyway, so:

Code:
sub RandInt()
    //returns a random integer between 0 and 32767
    a = val(system$("echo $RANDOM"))
    return a
end sub

sub RandInt0To9()
    //returns a random integer between 0 and 9, inclusive
    a=val(mid$(time$, 8,1))
    return a
end sub
Reply
#4
I think this has been fixed. it seems that yab seeded rand twice, once with an int that didn't work so well. I commented out that line and now it seems to work as documented. one needs to use GetLatest-yab.zip to try this out.
Reply
#5
Hello together,

i use the ran() function as following:


for i=1 to 40
x=int(ran(14))
print x
next i

so i get numbers from 0 to 14 (but not 14).

It works fine.

best regards

lorglas
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)
Free Web Hosting