Random numbers

Questions about the BASICtools and MakeItC
Post Reply
YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Random numbers

Post by YahooArchive »

>from the help line
>I need a random number generator, rnd() to include in a Basic type program to
run on a SuperProPlus. I see that the rnd() function has been moved to the
pbasic.lib library. Where can I obtain a copy of that peogram or library?

Yes we did take random out of the firmware, to make room for floating point
(especially in the BASICchip where we're down to 50 bytes in the allocated 12K).

And we need to publish that routine in BASIC. There may be better ones out
there especially with floating point, but what was in there was-

FUNCTION random(seed)
seed *= 279470273
return seed MOD &HFFFFFFFB
END FUNCTION

My question is, I've been writing code for 35 years and other than classroom
exercises I've never needed a random number. What do people use it for?



YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: Random numbers

Post by YahooArchive »

Most games need some kind of random generator, TCP incorporates a random backoff
time delay between a failed packet and resend request (to keep the sender and
receiver from predictable collisions in resend sequences), Quote of the Day
daemons often randomize rather than return quotes in a predictable order...

Basically anywhere it's desirable to breakup simple, mundane, easy-to-recognize
patterns, by replacing them with complex, hard-to-recognize ones, you'll find
random number generators. :-)

-MM

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: Random numbers

Post by YahooArchive »

As I don't write games often or do network back off algorithms, I have yet to
ever need a random number.

The version I published is not working, (mod with negative number is returning a
simple sequence). So I'm looking for input from the community.

A simple thing I might suggest is just read the TIMER when user input is
received. It counts in microseconds, and if you use the low order bits, I doubt
anyone has good enough rhythm to be able to generate non-random values.

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: Random numbers

Post by YahooArchive »

There are a couple simple ones
herehttp://en.wikipedia.org/wiki/Random_number_generation and they suggest
seeding with the RTC value.

Googling (Design of pseudo Random number generators) will get you more.

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: Random numbers

Post by YahooArchive »

// This works surprizingly well. ... I am shocked at how
// good the distribution is. Take a peek at the following image
// http://gyazo.com/83546e60d2dfeaa97b0ddc41f4edc269.png
// I got the code from this site and ported it to ARMbasic.
// http://www.bobwheeler.com/statistics/Pa ... iaPost.txt

/* Adapted to 31bit space since AB uses singed ints and shifting
signed negatives retains the sign vs. treating them like unsigned... */

/* Global static variables: */
dim _z, _w as integer // used by mwc random number generator

function znew
_z=36969*(_z and $ffff)+(_z>>16 and $ffff)
return $7fffffff and _z<<16
endfunction

function wnew
_w=18000*(_w and $ffff)+(_w>>16 and $ffff)
return _w and $ffff
endfunction

function mwc
return (znew+wnew)
endfunction

function rand as single
dim u as integer
// Produces a random sample from the open interval (0, 1).
// The method will not return either end point.
// 0 <= u < 2^31
u = mwc
// The magic number below is 1/(2^31 + 2).
// The result is strictly between 0 and 1, exclusive of either.
return (u + 1.0) * 4.656612868740580000E-10
endfunction

sub init_globals
' _z = 362436069 ' default static seed
_z = timer and $7fff0000 '<- use a static seed if you want repeatability...
_w = 521288629
endsub

main:

dim x as integer

init_globals

// this loop creates 100000 pairs of random numbers
// I did this to copy and paste into excel and then do a graph
// to demonstrate the distribution
// a image of the graph is shown here:
// http://gyazo.com/83546e60d2dfeaa97b0ddc41f4edc269.png
for x = 1 to 100000
Print rand, rand
next x


end

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: Random numbers

Post by YahooArchive »

Also, the source and a spreadsheet were uploaded to the files section in June:

Enjoy.

-t
Attachments
IEEE 754 Random Function.zip
(929.99 KiB) Downloaded 1328 times
Last edited by YahooArchive on Wed Feb 13, 2013 2:10 pm, edited 1 time in total.

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: Random numbers

Post by YahooArchive »

In the early 90's we used to use the timer as the seed in Quick Basic with the
following command.

RANDOMIZE TIMER

The timer as a seed value is fairly common as I think someone else recommended
the timer.

Post Reply