Random Numbers? - Script Writing - CHDK Forum

# Random Numbers?

• 23 Replies
• 13180 Views

#### ab

• 17
##### Random Numbers?
« on: 28 / April / 2008, 18:15:36 »
Is there a way to have Ubasic generate random numbers?  If so that would be great!

Could I call out to the system clock at least and multiply through that to generate quasi-random numbers?

#### fbonomi

• 469
• A570IS SD1100/Ixus80
##### Re: Random Numbers?
« Reply #1 on: 28 / April / 2008, 18:30:39 »
yes, you could... how accurate must this random generator be?

How often must you get those random numbers?

What range of nymbers do you need (how many different vakues yiu need)?

#### ab

• 17
##### Re: Random Numbers?
« Reply #2 on: 28 / April / 2008, 18:37:39 »

#### waldo

• 238
##### Re: Random Numbers?
« Reply #3 on: 28 / April / 2008, 19:40:14 »
From your description, I would think a pseudo-random sequence of 8 bit numbers that repeats every 256 values would suffice.  If so, consider using a Linear Feedback Shift Register (LFSR).  It's a very simple way of creating an arbitrarily long sequence.  Here's code for a CHDK script that does an 8-bit LFSR:

Code: [Select]
`@title LFSR Test rem 8 bit lfsr test rem c contains pseudo-random number c = 1 for g=0 to 1000 gosub "lfsr8" print c sleep 100 next g end :lfsr8 if (c & 1) = 1 then c = (c/2) ^ 142 else c = c / 2 endif return`
Have fun !
« Last Edit: 28 / April / 2008, 19:42:17 by waldo »

#### ab

• 17
##### Re: Random Numbers?
« Reply #4 on: 28 / April / 2008, 19:46:56 »
How big are those numbers going to be???

#### waldo

• 238
##### Re: Random Numbers?
« Reply #5 on: 28 / April / 2008, 22:39:48 »
0 to 255.  You can scale them down with division or use more bits in the sequence for a larger range.

#### PhyrePhoX

• 2254
• make RAW not WAR
##### Re: Random Numbers?
« Reply #6 on: 29 / April / 2008, 00:53:45 »
you can also use my build from http://chdk.setepontos.com/index.php/topic,978.0.html, it has (among other stuff) a random function for ubasic. someday in the future i guess it will be incorporated in the official build

#### ab

• 17
##### Re: Random Numbers?
« Reply #7 on: 29 / April / 2008, 01:13:38 »
I posted some preliminary results over on my blog ---

… timelapse focus wobble script … @ Arman Bohn

PhyrePhox - I'll look into your random function next.

#### PhyrePhoX

• 2254
• make RAW not WAR
##### Re: Random Numbers?
« Reply #8 on: 29 / April / 2008, 01:27:43 »
well, i guess waldo's script does it for you already
my build could help though when you need to write a smaller script (also it should be like 50 ms faster each time random is executed, to the least), also i guess it is "more random", as it uses the tickcount as seed.
looking forward to seeing your video, looks very promising. i also might add that i quite like your music

#### ab

• 17
##### Re: Random Numbers?
« Reply #9 on: 29 / April / 2008, 01:44:37 »
Thanks!

I've been working on my first full length in 10 years.  Having a blast with an A640 and the scripts.

How can I get your firmware into my cam?