How to generate a random number between fixed points

pottsy1000000

New Member
Hi everyone,

Can someone help me with a code question? I need to generate a random number between two fixed points. e.g. give me a number between 1 and 20. I've tried the Blockly 'set variable a to random', but it always seems to give me a 3 and I cant specify the min and max.

Any ideas please?
 

pottsy1000000

New Member
Hi Edmunds, thanks for the reply. This is certainly along the lines of what I need. However, if I insert that code, I just get an error. I'm pretty new to this, so may not have fully understood how to implement the solution.

If i can give you some more contenxt, I have an MP3 player and I want to randomly play a single track that is on it. At the moment I can only say to play a specific track number. Here is the code;

Code:
main:
	high B.2
	high B.5
	high B.3
	high B.4
	pause 1
	setfreq m8
	serout B.4, T9600_8, ($7E, $FF, $06, $09, $00, $00, $02, $EF)	;SPE035 initialise
	setfreq mdefault
	pause 100
	pause 1000
	setfreq m8
	serout B.4, T9600_8, ($7E, $FF, $06, $06, $00, $00, 22, $EF)	;SPE035 volume
	setfreq mdefault
	pause 100
	setfreq m8
	serout B.4, T9600_8, ($7E, $FF, $06, $12, $00, $00, 1, $EF)	;SPE035 play
	setfreq mdefault
	pause 100
	do
		pause 10
	loop until pinC.2 = 1
	toggle B.2
	pause 1000
	toggle B.2
	toggle B.5
	pause 1000
	toggle B.5
	low B.2
	low B.5
	low B.3
	stop
So Im just always playing track 1, and dont know how to get a random number between x and y
 

hippy

Technical Support
Staff member
The following is untested but should play a random track -

varA = Raw random number
varB = Lowest track number wanted (20)
varC = Highest track number wanted (30)
varD = Difference (30 - 20 = 10)
varE = Modulo to obtain difference (10 + 1 = 11)
varF = Random number; 0 to difference (random % 11 = 0 to 10 )
varG = Track number ( 0 to 10 + 20 = 20 to 30 )

That could be optimised so not so many variables would be needed.
 

Attachments

pottsy1000000

New Member
The following is untested but should play a random track -

varA = Raw random number
varB = Lowest track number wanted (20)
varC = Highest track number wanted (30)
varD = Difference (30 - 20 = 10)
varE = Modulo to obtain difference (10 + 1 = 11)
varF = Random number; 0 to difference (random % 11 = 0 to 10 )
varG = Track number ( 0 to 10 + 20 = 20 to 30 )

That could be optimised so not so many variables would be needed.
Hi Hippy, that looks great, thanks. I'll try it tonight and let you know how I get on.
 

pottsy1000000

New Member
Hi Hippy, that looks great, thanks. I'll try it tonight and let you know how I get on.
Hi Hippy, sorry for taking so long to get back to you. I've implemented this code, but it always seems to generate the same number. I've had a look at the simulation results and the problem seems to be that the Set VarA to Random seems to always generate the same random number, therefore, the rest of the calculation naturally comes up with the same random number within the min and max parameters set.

Any ideas what might be causing this? Here's a screen shot of the code and simulation.

Untitled.png
 
Top