Random Numbers

DoomDuck

New Member
Hello,

I wrote the simplified code below so that one of four light displays would appear randomly and it works perfectly in the simulation, but when I program my 28x PICAXE, it doesn't seem to work and only the collapse segment of the code continously loops. Where did I go horribly wrong?

thanks,
DD

Code:
main:
	random w0
	if w0 > 30000  then random1
	if w0 < 30000 then random2
	goto main
	
random1:
	random w1
	if w1 > 30000 then chain
	if w2 < 30000 then alternating
	goto main
	
random2:
	let w2 = timer
	random w2
	if w2 > 30000 then wash
	if w2 < 30000 then collapse
	goto main

collapse:
	high 4
	wait 2
	low 4
	goto main
	
wash:
	high 1 
	wait 2
	low 1
	goto main
	
chain:
	high 7 
	wait 2
	low 7
	goto main
	
alternating:
	high 0
	wait 2
	low 0
	goto main
 

eclectic

Moderator
@DD.
Until you get a more knowledgeable reply,


1. The following modifications might help

Code:
main:
	random w0
	if w0 > 30000  then random1
	if w0 < 30000 then random2
	goto main
	
random1:
	random w1
	if w1 > 30000 then chain
	if w1 < 30000 then alternating ; ***********
	goto main
	
random2:
;	let w2 = timer ********
	random w2
	if w2 > 30000 then wash
	if w2 < 30000 then collapse
	goto main

collapse:
	high 4
	wait 2
	low 4
	goto main
	
wash:
	high 1 
	wait 2
	low 1
	goto main
	
chain:
	high 7 
	wait 2
	low 7
	goto main
	
alternating:
	high 0
	wait 2
	low 0
	goto main
2. However, do you simply want four outputs (7,4,1,0) to light randomly?

If so, here's a long-winded version which might help

Code:
#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

e
 

hippy

Technical Support
Staff member
There are two problems - RANDOM creates a pseudo randon number and all random numbers in variables will follow the same sequence. I'm afraid my brain's not in gear at this time of evening to work out a solution.
 

ljg

New Member
There are two problems - RANDOM creates a pseudo randon number and all random numbers in variables will follow the same sequence. I'm afraid my brain's not in gear at this time of evening to work out a solution.
generate random numbers in a loop until some external stimulus value is met, i.e. stay in a generating loop until a button is pressed to get a random seed for w0


alternatively, use the result of reading a scaled value as the seed (light level, battery voltage, sound level, discharge rate of an air gap capacitor, etc.

If it's a robot, do some math on the sensor values for your sonar, IR distance sensors, tilt sensor, GPS time reading, etc. and use that as a seed. a Sharp analog distance sensor without a big smoothing cap would be particularly jumpy.

external stimulus is sure to be more random, and the random seed will be different when you need it to be.
 
Top