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.

Thanks in advance :)

Hello Kelpy!I’m a noob so

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
http://www.picaxeforum.co.uk/showthread.php?t=7799

 

renkku

Even better here!

Even better here!

Great!

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!)

Natural Random

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 …

Divide doubles and cast as an int

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.

picaxe

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.

**Didn’t know that. **

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. 

Thank you, fellas

Thanks for all your input.

I have tried the link that renkku sent me, and it seems to work fine. I set it running in the simulator for about 10 mins, and it did seem to output random numbers between 3 and 6, which is what I need, and the code is truly uncomplicated :slight_smile:

for b4 = 1 to 100
do
RANDOM w0
b2 = w0 / 199 // 6 + 3
LOOP UNTIL b2 <> b3 and b2 < 7
b3 = b2
sertxd("The value of b3 is ",#b3,13,10)
next b4

I did have to add in the condition that b2<7 though, as 7 and 8 appeared occasionally. Perhaps I should change the 199 to another number. I’ll have a play.