Can the "random" function be limited to a range of numbers?

OLDmarty

Senior Member
Hi All,

I'm just starting to tinker with the Random command to light up 4 LEDS in any combination from $0h to $Fh.

As i'm only using 4 bits to drive the LEDs, the random word seems to spend a lot of time generating big numbers larger than $Fh, so my LEDs are often in the OFF state.

Can the random command be told to only generate numbers within a given range? or would i check if the number is greater than $Fh and then divide it down until it is equal (or less than) $Fh ?

I recently thought about using a lookup/lookdown array for my 16 values, but of course the large random word values prevent looking for the correct number in my lookup/lookdown selection.

I'm not sure what my options are now for something that seemed like an easy idea when i first began trying some code with the random command, now i've stuck myself in a hole lol.

with thanks in advance...
 

papaof2

Senior Member
Does your project include a real time clock? If so, you could read the seconds and do a little math (divide seconds by X, shift them left/right X columns, multiply by 2 and then divide by 3, etc.) to get a semi-random number. Save the number used and ensure the "new" number is different by some value - that "random" number might be something else to save to ensure you don't have adjacent number repeats in your pseudo-random numbers (unless that works well for the pattern(s) you want).

You may find other ideas for how you'd want to process a 8-bit "random" number: use the first 4 bits, the last 4 bits, the "middle" 4 bits, the odd bits, the even bits and as much "and so forth" as you want to play with ;-)
 

inglewoodpete

Senior Member
The easiest way to create a random number within limits is to use the // opertor (modulus divide). The Random function (command) must always have exclusive use of a word variable. For the 16-value (4-bit) result, use a byte variable.

Try the following piece of code in the simulator:
Rich (BB code):
#PICAXE 08M2
Do
   Random w12
   b5 = w12 // 16
   SerTxd (#w12, " ", #b5, CR, LF)
Loop
 

AllyCat

Senior Member
Hi,

If you want exactly 16 combinations, then the easiest is to simply AND the Word value with 15 (i.e. %1111). Note that you MUST use a Word variable and it will always generate exactly the same sequence from any given starting seed (e.g. 0). More than you'll probably ever require is HERE. ;)

Cheers, Alan.
 

bpowell

Senior Member
The easiest way to create a random number within limits is to use the // opertor (modulus divide). The Random function (command) must always have exclusive use of a word variable. For the 16-value (4-bit) result, use a byte variable.

Try the following piece of code in the simulator:
Rich (BB code):
#PICAXE 08M2
Do
   Random w12
   b5 = w12 // 16
   SerTxd (#w12, " ", #b5, CR, LF)
Loop
I'd change the following line:

Rich (BB code):
b5 = w12 // 0xF + 1
That way reads better to me, as it says the random number will be UP TO (and including) 0xF

That could just be a personal preference though ...
 

AllyCat

Senior Member
Hi,
Rich (BB code):
b5 = w12 // 0xF + 1
Yes that will generate a maximum of 15, but can never generate 0, so gives only 15 combinations.

Sometimes a range of (say) 1 to 16 will be desired, but here the OP has specifically requested 0 to $F so either // 16 or AND 15 , etc. is correct (or the equivalent Hex or Binary formats).

Because of the nature of the FeedBack Shift Register algorithm, it's best to use the LSBs (if starting from a small seed value), but for a "better" 4-bit random sequence, the RANDOM should be executed at least 4 times (however it's a surpringly "fast" executing instruction).

Cheers, Alan.
 
Top