Random number seed


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2007
    Posts
    8

    Default Random number seed

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Two questions:

    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...

    You should change it as follows.
    Code:
    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.


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

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,611


    Did you find this post helpful? Yes | No

    Post

    Hi,

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

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Sorry to jump in.....

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    You seem to be having it correct Henrik.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  6. #6
    Join Date
    Jan 2007
    Posts
    8


    Did you find this post helpful? Yes | No

    Default Random number seed

    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?
    Last edited by kwelna; - 24th January 2007 at 04:08.

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,611


    Did you find this post helpful? Yes | No

    Default

    Hi, Kwelna

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

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  8. #8
    Join Date
    Jan 2007
    Posts
    8


    Did you find this post helpful? Yes | No

    Default Random seed

    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

  9. #9
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by kwelna
    that is what happens when you get old!
    Imagine when you'll be as old as i am

    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....
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  10. #10
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,611


    Did you find this post helpful? Yes | No

    Talking Stone age survivors ???

    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/for...932b79b20ee1c5



    Alain
    Last edited by Acetronics2; - 1st February 2007 at 11:38.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  11. #11
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    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.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Random Numbers
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 18th December 2008, 09:47
  2. Random seed using the ds1307 RTC
    By rookie11 in forum General
    Replies: 7
    Last Post: - 25th November 2007, 21:31
  3. Working with the random number generator
    By kwelna in forum mel PIC BASIC
    Replies: 9
    Last Post: - 16th January 2007, 18:50
  4. Random number results
    By bartman in forum mel PIC BASIC
    Replies: 3
    Last Post: - 9th March 2005, 18:39
  5. more random number questions
    By bartman in forum mel PIC BASIC
    Replies: 1
    Last Post: - 14th November 2004, 18:55

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts