Random Numbers

ChedderCheeser

Senior Member
Hi guys. I am trying to figure out how I can create RANDOM numbers within a certain range. The first one (w0) needs to be between 10 and 50. The second (w1) needs to be between 25 and 175. Any ideas?
 

Jamster

Senior Member
Inacurate but here is how i do it

Random creates a number between 0 and 65535 so:
65535/1638 Creates a number between 0 and 40
Ans+10Creates a number between 10 and 50
65535/436.9Creates a number between 0 and 150
Ans+25Creates a number between 25 and 175
 

BillyGreen1973

Senior Member
Inacurate but here is how i do it

Random creates a number between 0 and 65535 so:

65535/436.9

Creates a number between 0 and 150

Ans+25


Creates a number between 25 and 175
Not possible directly due to integer maths only.

You could look at the 'min' and 'max' commands to limit the results within a certail range
 

nick12ab

Senior Member
possibly a bit inefficient, but
Code:
main:
   random w0
   if w0 < 25 or w0 > 75 then main
Exchange w0 and main for any other variable/label. Code doesn't advance until the number is between 25 and 75 inclusive.
 

eclectic

Moderator
Based on hippy's thread in post #2

Works on a real 08M2

Code:
#picaxe 08M2
pause 1000

main:
For b10 = 0 to 255
RANDOM w0
 w1 = w0 // 150 + 25
 
 sertxd (#W1,", ")
 
next
e
 

srnet

Senior Member
Hi guys. I am trying to figure out how I can create RANDOM numbers within a certain range. The first one (w0) needs to be between 10 and 50. The second (w1) needs to be between 25 and 175. Any ideas?
You cant.

PICAXE has a 'pseudo' random generator, which may be good enough for you. Its important to seed the number correctly, or each time you start it you may (should really) get the same sequence. Easiest way would be to check the status of an external event, such as a button press, before starting the routine.
 

nick12ab

Senior Member
I decided to run a test on a 40X2 to see what the sequence was. This was the sequence after running it for a while:
With word variable:
Code:
0
2
5
11
23
47
94
189
378
756
1512
3024
6049
12098
24197
48395
31254
62509
59482
53429
41323
17110
34221
2906
5812
11624
23248
46497
27459
54919
44303
23071
46143
26750
53501
41467
17399
34799
4063
8127
16255
32511
65023
64510
63484
61433
57330
49125
32714
65429
65323
65110
64684
63832
62128
58720
51905
38275
11014
22028
44057
22578
45156
24776
49552
33568
1601
3202
6405
12811
25623
51247
36959
8382
16765
33530
1525
3050
6101
12202
24405
48810
32084
64168
62801
60066
54596
43656
21777
43555
21574
43148
20760
41521
17506
35013
4490
8981
17962
35925
6315
12631
25263
50526
35516
5497
10995
21990
43980
22424
44849
24162
48325
31114
62229
58923
52310
39084
12632
25265
50531
35526
5517
11035
22070
44141
22746
45492
25449
50898
36261
6986
13973
27946
55893
46251
26966
53933
42330
19125
38250
10964
21928
43856
22176
44352
23169
46338
27140
54281
43027
20519
41038
16541
33082
629
1258
2517
5034
10069
20138
40277
15019
30039
60079
54623
43710
21884
43768
22000
44001
22467
44935
24335
48670
31804
63608
61680
57824
50113
34691
3846
7692
15385
30771
61542
57549
Ran it a second time and the exact same sequence was reproduced so seeding is very important.

With byte variable:
Code:
0
2
5
11
23
47
94
189
122
244
232
208
161
66
133
11
23
47
94
189
122
244
232
208
161
66
133
11
23
47
94
189
122
244
232
208
161
66
133
11
23
47
94
189
122
244
232
208
161
66
133
11
23
47
94
189
122
244
232
208
161
66
133
11
23
47
94
189
122
244
232
208
Just a shorter sequence.
 

inglewoodpete

Senior Member
When working with random numbers, never use a byte variable with the RANDOM command. Never use MIN and MAX as they polarise the values. Preserve the value of the random register.

If you have an ADC input in use, use it to create the seed. If you don't then use a spare ADC input pin (left open circuit) as a seed. If need be, add a length of insulated wire to the input. One of the rare times that noise (inconsistent readings) is good.
 
Last edited:

graynomad

Senior Member
It's almost impossible to get random numbers with a processor. Any given RND-style function will produce exactly the same sequence for a given seed. So you need a random seed.

Back to square one.

You need what is called an "entropy source" ie a source of truly random events and normally there are none on an embedded controller. If you have buttons or something that a human is pressing or some other "random" event then that can be used by timing the event, but if not you are out of luck.

Re the unconnected ADC input, you can't just read the ADC, that's decidedly non-random. If you take say 100 readings and accumulate bit 0 into an array then "whiten" the array (basically dedupe the bits) and grab a couple of bytes from the array to use as your seed that works OK. But there is still definite grouping (at least on an AVR). Here's an example of the distribution using this technique (about 11000 samples, each reading the ADC up to 1000 times)

rng_dist5a.jpg

Note the distinct banding. I'm still not sure what causes it but general discussion on the Arduino forum concluded that all sorts of things influence the reading, including internal signals. It may also have been the whitening algorithm.

Having said all that I find the ADC method to be "random enough" for my purposes, ie that of generating a unique serial number for a PCB at run time. If you just want to shuffle your MP3 songs it's overkill, and if you need full on encryption it's nowhere near enough.
 
Last edited:

fernando_g

Senior Member
If you have an ADC input in use, use it to create the seed. If you don't then use a spare ADC input pin (left open circuit) as a seed. If need be, add a length of insulated wire to the input. One of the rare times that noise (inconsistent readings) is good.
Of course, if your noise level is too low, you may want to precede your input with a "noise amplifier" :eek:

Now seriously, a back-biased base-emitter junction is a surprisingly good noise generator. Couple that tho your ADC input and voila! a quite good random number.
 
Top