PDA

View Full Version : Random number seed



kwelna
- 22nd January 2007, 23:56
Thank you for all our help on my last post. It all really helped. To be honest, I finially just looked at the generated number and then based on its value fired off one of 5 sub routines. Here is the chunk of code i am using....Not very elegant but it works fine.

Begin:
Count = 0
W0 = W0 + 1
Peek PORTB, B2
IF B2 = %10000000 Then Test
GoTo Begin

Test:
Count = Count + 1
IF Count = 6 Then Begin
Random W0

IF W0 < 13105 Then loop1

IF W0 < 26210 Then loop2

IF W0 < 39315 Then loop3

IF W0 < 52420 Then loop4

IF W0 <= 65525 Then loop5

loop1:
Toggle 0 'Turn on LED connected to RB0
Pause 500 'Delay for .5 seconds
Toggle 0 'Turn off LED connected to RB0
GoTo TEST


Now I am on to my latest issue. First off I am using Pic Basic (The low cost version.) I am trying to seed the random number generator with a changing opening amount. So each time I use the device, the initial random pattern changes.

My queston is, the EEprom memory specifically location "0" should keep its value after the pick is powered down and then powered up again right??

So all I have to do is set an additional counter and incrament the seed by setting memory loaction 0 to this counter.

sayzer
- 23rd January 2007, 08:52
Two questions:

1. In your code above, you have the following.


IF W0 < 13105 Then loop1
IF W0 < 26210 Then loop2
IF W0 < 39315 Then loop3
IF W0 < 52420 Then loop4
IF W0 <= 65525 Then loop5


Lets take W0=10000 then all of these IF statements will be true and be executed. Or say W0=28000; in this case last three IF statements will be executed, etc...

You should change it as follows.


IF W0 < 13105 Then
loop1
ELSE
IF W0 < 26210 Then
loop2
ELSE
IF W0 < 39315 Then
loop3
ELSE
IF W0 < 52420 Then
loop4
ELSE
IF W0 <= 65525 Then loop5
ENDIF
ENDIF
ENDIF
ENDIF





OR use a select case, it would make it easier to follow the logic.

2. Forget this one.


---------------------------

Acetronics2
- 23rd January 2007, 12:45
Hi,

There was a thread about random numbers, here ... sometimes ago.
Conclusion was Microchip offers the best routine ( assembler ... yes ! ) in its applications.

Alain

HenrikOlsson
- 23rd January 2007, 14:19
Hi,
Sayzer wrote:


1. In your code above, you have the following.

Code:
IF W0 < 13105 Then loop1
IF W0 < 26210 Then loop2
IF W0 < 39315 Then loop3
IF W0 < 52420 Then loop4
IF W0 <= 65525 Then loop5

Lets take W0=10000 then all of these IF statements will be true and be executed. Or say W0=28000; in this case last three IF statements will be executed, etc...

Why is that?
If W0=10000 the first IF statement tests true and the program jumps to the Loop1 routine which is executed. At the end of Loop1 the program jumps to Test where a new RANDOM is executed and assigned to W0. Lets say it's 28000 as in you example. The third statement tests true and the program jumps to Loop3, executes it and then starts over with another RANDOM. At least that's how I read it.

HTH
/Henrik Olsson.

sayzer
- 23rd January 2007, 14:28
You seem to be having it correct Henrik.

kwelna
- 24th January 2007, 03:02
Actually the if statements work fine, but I like adding the "Else" statements and "EndIf"s to clean up the code (Thank you Sazer). and comments to clarify the logic behind my code. My real issue is the way the code is now, my code always provides the same sequence of numbers on start up. I need it to be different each time at startup as well as any other time it basically is supposed to randomly flash 1 of 5 different LEDs - 5 times and then stop and wait for B.7 to go high and then star again. So how can I randomly add a seed to the very first iteration of this process. I read some where that you can pull a value from TMRO, so it will give you an initial seed of 0 to 255. Any thoughts?

Acetronics2
- 31st January 2007, 13:24
Hi, Kwelna

Just keep the last value before switching off in EEPROM ... as a new seed !!!

Alain

kwelna
- 1st February 2007, 00:14
Duh!!! So simple I never would have thought of it.........well maybe in a week or two.... I am alwqys looking for a more complicated method. that is what happens when you get old!

Thanks again

Kevin

mister_e
- 1st February 2007, 00:30
that is what happens when you get old!

Imagine when you'll be as old as i am :D

You could still use an hardware solution... count pulses at the output of a white (even pink) noise generator... now it's going to be really random. Always a long debate around this....

Acetronics2
- 1st February 2007, 09:00
Hi, Steve

You were talking about the MM 5837 ... I suppose ???

The only one corresponding to your age LOL !!!


Hi, Kevin

<< I am alwqys looking for a more complicated method. that is what happens when you get old! >>

Here, you're only talking about yourself ...

Ah, Will you have a look here ???

http://www.manhattancontrols.com/forums/circuit-ed/viewtopic.php?t=46&sid=8726168d6c891b196f932b79b20ee1c5



Alain

mister_e
- 1st February 2007, 17:33
Hi, Steve

You were talking about the MM 5837 ... I suppose ???

The only one corresponding to your age LOL !!!


LMAO! well could also be a simple op-amp (maybe dual), cmos gate etc etc. no need for a dedicated IC. Maybe PBP SOUND could do the job.