# Question about Picaxe Random function

I understand that the picaxe does not have a clock, and that the "random" function will produce numbers in a sequence between 0 and 65535 (not really random), but I have a question.

From memory, when I used commodore basic all those years ago, you could write something like rnd(9), which would produce a random number between 0 and 9 (I think that's right), so is there any way I can get picbasic to do the same. I want to switch, on and off, 4 led's in a random sequence.

## Comment viewing options

Hello Kelpy!

I'm a noob so wait for someone else to answer, but

I don't know any command in picaxe basic that would function as a random generator between given numbers, but you could always split the whole interval in as many numbers you wich to make and have different subs for them.

I found this here

#picaxe 28X1

main:
random w0
w1 = w0 //4 ; see #rem below
on w1 gosub zero , one, four, seven
goto main

zero:
high 0
wait 1
low 0
return

one:
high 1
wait 1
low 1
return

four:
high 4
wait 1
low 4
return

seven:
high 7
wait 1
low 7
return

#rem

renkku

Even better here!

Many thanks for that, renkku.

I have to say, at the moment it is as clear as mud (to my tired old brain), but I think I can see how it would work.

I'll have a play around with it tomorrow (off out in a minute, doh!)

Another way to generate a random number is to set up a constant clock (a byte that is inc'd at the end of every Main loop, for example). when it gets to 9, just reset it to 0. When something happens that requires the random number, a select case then acts based on the variable's current value (which was determined externally).

Calculon hopes this makes sense ...

If you want between 0 and 9 (10 numbers) divide the output of the random function by 6553.5 and cast as an integer. If you want to avoid messing with the clock yourself and need a different range just follow this formula;

RandMax = Max value of Random function; // In your case 65535 but this depends on operating system, hardware in other cases.
Range = Max required number;
Val = returned intiger value within required range;

int Random= RandomFunction();
int Val = Random/ [(int)RandMax/(Range + 1)] // (The +1 accounts for zero as a number ie 0-9 is 10 numbers)

This divides the maximum range of the random function into the required number of sets and then a value generated by the random function is determined to be part of some set. Each set is associated with a possible required value. This functions may end up being slightly biased depending on what the range is because it is possible that the maximum value of the random function cannot be evenly divided into the required number of sets - but with sets that are 6553 values large this should not effect your functionality beyond 0.01%.

ie
0 = 0 - 6553
1 = 6554 - 13107
2 = 13108 - 19662
...ect.

Calculon assumes that Gonzik is used to arduino/avr/other c-based mcu's. picaxes can't calculate decimals. The rev-ed manual shows a possible workaround to this, so the logic is sound, but calculon has never needed this workaround before.

Thanks Calculon I didn't know. I knew microchip can be programmed in C but I didn't know picaxe has no decimal support. He could still do integer division and that would give him random values within his range alas the distribution would not be entirely even.