randomseed

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

Post Reply
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

randomseed

Post by dunbarx » Sat Apr 21, 2012 5:43 am

This topic came up in the thread on opening random cards. I am putting my query in the beginner section because that is how I feel right now.

It was mentioned that the randomseed is, unless meddled with, fixed for any session, which is true. it was asserted that a string of random numbers generated with this seed would indeed be random, but also that this string would be the same for each call using that seed. The dictionary confirms this.

I have never seen this behavior. In a button script with a "field 1" available somewhere:

Code: Select all

on mouseUp
   put ""  into fld 1
   repeat with y = 1 to 20
      put the randomseed & return after fld 1
       repeat 10
         put random(10) after temp
      end repeat
      put temp & return after fld 1
      put "" into temp
   end repeat
end mouseUp
Now, the randomseed stays the same, as advertised, but the string developed in each iteration is different, as my 25 years of experience using this function has always shown. It never occurred to me to have to worry about it. I understand changing the randomseed to increase the "randomness" of the function itself. What do I not understand about the assertion? What script would in fact generate a string of identical random numbers, over and over?

Craig Newman

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: randomseed

Post by Mark » Sat Apr 21, 2012 12:02 pm

Hi Craig,

The random numbers in LiveCode are comparable with a table of true random numbers. The random seed is where you start reading in the table. If the table is "5,4,3,6,2,8,1,9,7,8,0" and the randomseed is 4, then you start reading at 6 in the table and the next number is 2. This means that the randomseed changes while you read from the table, following the pattern 4,5,6...,1,2,3... Each time when you finish the table, you just continue counting at the start. This way, you make sure that you preserve the randomness of the table.

I know that this doesn't seem random to most people, but the purpose is to have a statistically random list of numbers, rather than an intuitively list of numbers. For most purposes in programming, statistical randomness is sufficient. If it isn't, then you should ask the user to throw dice, draw a card or something similar from the real world.

Key to randomness is to remember the randomseed. If you always start with the same randomseed 4 in the example above, then your list will always be 6,2,8,1,9,7,8,0,5,4,3, 6... and your numbers are no longer statistically random. Once upon a time in the past someone decided purely randomly to set the randomseed to a starting number and from that moment on LiveCode just kept counting each time when you use the random function. Because the first randomseed was a random number and the table itself consists of random numbers, you're always getting statistically random numbers.

(Just to be complete: I don't think that the random table is an actual table. I think it is a function, which the processor can executing directly, simulating the table and hence isn't completely random after all, but it is sufficient for programming).

Here's a simple demo:

Code: Select all

on mouseUp
     set the randomseed to 1234567890
     repeat 10
          put random(10) & comma after myList
     end repeat
     put item 1 to -2 of myList
end mouseUp
This script always returns the same "random" numbers. They are still statistically random, but because they are always the same they are probably not suitable for programming.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: randomseed

Post by FourthWorld » Sat Apr 21, 2012 4:04 pm

Mark's explanation is a good one. The only thing I would add to that is that setting the randomSeed is usually most useful only when you want to ensure that the simulated randomness follows a fixed pattern (all randomness is computers is simulated, since computers can't do anything truly random).

For example, if you want to repeat a particular seemingly-random sequence, you can set the randomSeed to the same number in each use.

For most normal purposes, leaving the randomSeed alone will suffice.
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: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: randomseed

Post by dunbarx » Sun Apr 22, 2012 8:23 pm

Mark, Richard.

Thanks. The difference can be illustrated as follows. This generates different strings:

Code: Select all

on mouseUp
   put "" into fld 1
    set the randomseed to 1234567890
   repeat 2
           repeat 10
                   put random(10) & comma after myList
           end repeat
      put return after myList
   end repeat
        put item 1 to -2 of myList into fld 1
end mouseUp
This generates the same string:

Code: Select all

on mouseUp
   put "" into fld 1
   repeat 2
    set the randomseed to 1234567890
           repeat 10
                   put random(10) & comma after myList
           end repeat
      put return after myList
   end repeat
        put item 1 to -2 of myList into fld 1
end mouseUp
The difference is subtle, at least to me.

Craig

Post Reply