# SimpleRandomWithNumberRange

#### Digisapien

##### Member
I read a lot of answers, and sooo many din't work very well. but I think I did find a way that seems to work okay. It was kind of simple.
random1:
for counter = 1 to 225
random counter
servopos B.5,counter
pause 1000
next counter
return

#### bpowell

##### Senior Member
What is our objective here? To contain random between 1 and 225? This program won't work for that ... at the "Random Counter" line; counter can be assigned a value > 225, and then the servo is driven there ... at the next iteration of the loop, the loop will drop out since the number was greater than 225 ... but it was still that value for a bit.

Further, each time you call this routine; you'll get the same series of numbers, since you're initially seeding "counter" with "1" to start.

Last edited:

#### pxgator

##### Senior Member
From the Picaxe command manual:

RANDOM wordvariable - Wordvariable is both the workspace and the result. As random generates a pseudo-random sequence it is advised to repeatedly call it within a loop. A word variable must be used, byte variables will not operate correctly

#### bpowell

##### Senior Member
From the Picaxe command manual:

RANDOM wordvariable - Wordvariable is both the workspace and the result. As random generates a pseudo-random sequence it is advised to repeatedly call it within a loop. A word variable must be used, byte variables will not operate correctly
It actually performs *worse* with a Word ...

#### bpowell

##### Senior Member
If you're just trying to constrain random numbers between a couple of values, here is a simple routine ... (it's a variable hog though!)

Code:
``````#Picaxe 08m2
#no_data

symbol randnum    = w1
symbol low_limit    = w2
symbol high_limit    = w3
symbol tries    = b8
symbol temp        = b9

setfreq m32

main:

do
low_limit = 50
high_limit = 500
tries = 20

gosub my_random
loop

goto main: ' Error handling ...

my_random:
for temp = 1 to tries
do
random randnum
loop until randnum >= low_limit AND randnum <= high_limit
sertxd ("Random number is: ",#w1,cr,lf);
next temp

sertxd ("Exiting the loop ...",cr,lf);

return``````

#### lbenson

##### Senior Member
it's a variable hog though

Less so if low_limit and high_limit (which don't actually vary in your example) are made constants.

#### Aries

##### New Member
This gives a sequence of random numbers in the range LowNumber to HighNumber (which can be variable)
In the simulator, readadc10 doesn't do anything useful, but it often works with an unallocated pin in real life.

random generates numbers in the range 0 - 65535.
Using ** means effectively multiplying the given numbers and then dividing by 65536.
So, to go from LowNumber to HighNumber inclusive, you need to increase the range by 1 and add back the start point at the end

Code:
``````symbol LowNumber = 1
symbol HighNumber = 255

symbol RandNumber = w1

for b0 = 0 to 50
random RandNumber
w2 = HighNumber - LowNumber + 1
w2 = w2 ** RandNumber + LowNumber
sertxd(13,10,#w2)
next b0``````

#### hippy

##### Technical Support
Staff member
Code:
``````Symbol counter      = b2
Symbol result       = b3
Symbol randomNumber = w2 ;  b5:b4

Symbol MIN_RANDOM   = 10
Symbol MAX_RANDOM   = 30

Symbol GAP_RANDOM   = MAX_RANDOM - MIN_RANDOM
Symbol MOD_RANDOM   = GAP_RANDOM + 1

For counter = 1 To 5
Random randomNumber
result = randomNumber // MOD_RANDOM + MIN_RANDOM
SerTxd("Result = ", #result, CR, LF)
Next``````