What algorithm for random() function?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

MichaelBluejay
Posts: 222
Joined: Thu Jul 01, 2010 11:50 am

What algorithm for random() function?

Post by MichaelBluejay » Mon Jul 12, 2021 6:45 am

I was not at all surprised that the Dictionary is completely silent about what algorithm the random() function uses.

For example, for Javascript, it depends on the web browser, but most seem to use xorshift128+.

Anyone know what algorithm LiveCode uses?

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9286
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: What algorithm for random() function?

Post by richmond62 » Mon Jul 12, 2021 6:53 am

Why is that important?

Surely it does not matter vis-a-vis LiveCode whether my car has
a diesel, petrol, propane or electric engine: the thing will still get me to Berlin.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9286
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: What algorithm for random() function?

Post by richmond62 » Mon Jul 12, 2021 7:42 am

Of course it might be useful to know if LiveCode's random numbers are really
random numbers or merely pseudorandoms.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: What algorithm for random() function?

Post by FourthWorld » Mon Jul 12, 2021 8:04 am

Randomness cannot exist in deterministic systems like computers (some argue that randomness doesn't exist anywhere in the universe as we know it).

So all computers simulate the concept of randomness with pseudorandomness.

Michael, I don't know offhand which pseudorandom algo LC uses, though I'm sure I can be discovered in the available source code.

But having devoted a fair bit of time to pondering randomness (for cryptography and games) I'm curious to learn what knowing the algo would provide you with.)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9286
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: What algorithm for random() function?

Post by richmond62 » Mon Jul 12, 2021 10:24 am

Randomness cannot exist in deterministic systems like computers
Quite: but a computer can pick up information from, for instance, a bit of Strontium as it decays.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: What algorithm for random() function?

Post by dunbarx » Mon Jul 12, 2021 7:57 pm

You can roll your own random generator with a package of kludges.

Take a (standard) random digit from the milliseconds. Do that as many times as the number of digits you need. True randomness comes from the human interaction with the time.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: What algorithm for random() function?

Post by FourthWorld » Mon Jul 12, 2021 10:46 pm

dunbarx wrote:
Mon Jul 12, 2021 7:57 pm
You can roll your own random generator with a package of kludges.

Take a (standard) random digit from the milliseconds. Do that as many times as the number of digits you need.
That's essentially what LC's random function does, using clock time for its default randseed.

Fun: if you need to have a pseudorandom sequence that's repeatable (such as for relatively even distribution), you can set LC's randseed to a given number, and subsequent calls to the random function will repeat exactly.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: What algorithm for random() function?

Post by dunbarx » Tue Jul 13, 2021 2:32 am

Richard.

I do understand that the randSeed is created from a virtually random source. But am I not correct in thinking that my silly little method would generate truly random numbers, by virtue of using something as "unstable" as the time, and a computer's having to live in the real world, to derive a truly random string?

Code: Select all

on mouseUp
   put "" into fld 1
   repeat 6
      put char random(10) of baseconvert(the milliseconds,10,10) after fld 1
   end repeat
end mouseUp
That extra stuff is because I have forgotten how to have LC give the milliseconds NOT in scientific notation.

Craig

MichaelBluejay
Posts: 222
Joined: Thu Jul 01, 2010 11:50 am

Re: What algorithm for random() function?

Post by MichaelBluejay » Tue Jul 13, 2021 2:50 am

A few years ago a professor created a new algorithm, PCG. On her website she details the various qualities desirable in an algorithm, and how the existing algorithms aren't as good as PCG. Granted that she could be biased, but I haven't seen any reputable criticism of her work or her claims.

I'd love to implement it in LiveCode, but I can't translate from the C-language example on the site, which is daunting.

Someone made a Javascript version, which is more understandable to me, but not completely understandable. Also, the author says that it requires a lot of code because Javascript doesn't handle 64-bit math natively like C does, and I imagine LiveCode would have that same limitation.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: What algorithm for random() function?

Post by dunbarx » Tue Jul 13, 2021 3:00 am

I remembered how I changed the scientific notation into a string:

Code: Select all

on mouseUp
   put "" into fld 1
   repeat 6
     put "" & char random(10) of the milliseconds after fld 1
   end repeat
end mouseUp
Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: What algorithm for random() function?

Post by FourthWorld » Tue Jul 13, 2021 4:11 am

Good stuff, Michael. Thanks for those links.

Hopefully someone with more familiarity than myself can chime on the algo LC uses.

I'm sure the professor's algo is a very good one, but what may be "best" seems dependent on the application.

For example, emulating true randomness well would allow for the same choice appearing over and over each time it's called.

But while that would fit the philosophical definition of "random", it may not fit a user's mental model of how randomness *should* work.

This talk describes different types of distributions of uncertain outputs for different board game design purposes:
https://youtu.be/qXn3tGBztVc

There what he calls "white" noise is what most programmers think of as fitting a reasonable set of expectations for randomness, offering somewhat even distribution of values.

But in the natural world (weather, stock markets) we find far more examples of "pink" noise, which may handle the same range of options as white but with a clustering of results near a base value, with outliers rare in proportion to their distance from the base, like a bell curve.

And because we have more direct experience with pink noise than white, many game systems strive for that sort of constrained randomness as it feels more "fun", or at least better fits player expectations (or maybe just player desires <g>).

When designing tabletop games these things can help inform choices for number of dice and range on each.

In a software program recently I found myself thinking about handling randomness in terms of multi-dice throws, and wrote some "pink noise" algorithms that almost literally employ dice dynamics under the hood, summing smaller pseudorandom outputs.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9286
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: What algorithm for random() function?

Post by richmond62 » Tue Jul 13, 2021 8:54 am

I was not at all surprised that the Dictionary is completely silent about what algorithm the random() function uses.
Why weren't you surprised?

MichaelBluejay
Posts: 222
Joined: Thu Jul 01, 2010 11:50 am

Re: What algorithm for random() function?

Post by MichaelBluejay » Tue Jul 13, 2021 5:55 pm

Because I rarely find what I'm looking for in the docs. That's why when I went to look up random(), I thought, "They're not even gonna bother to mention what algorithm they use." I was right for a reason.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9286
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: What algorithm for random() function?

Post by richmond62 » Tue Jul 13, 2021 7:49 pm

I was right for a reason.
And what was that reason?

As most of us are aware that computers are just clunky machines that are (and should stay) the slaves of
humankind we are also aware that computers don't have hands and oposble thumbs so they cannot chuck dice around
and any 'random' number they come up with has got to be a fudge.

I would be very surprised indeed . . .
-
SShot 2021-07-13 at 21.46.59.jpg

MichaelBluejay
Posts: 222
Joined: Thu Jul 01, 2010 11:50 am

Re: What algorithm for random() function?

Post by MichaelBluejay » Tue Jul 13, 2021 9:55 pm

richmond62 wrote:And what was that reason?
I just told you. It's literally every sentence of my reply. That was the whole point.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”