Random Numbers


Closed Thread
Results 1 to 13 of 13

Thread: Random Numbers

Hybrid View

  1. #1
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by BobEdge View Post
    I am trying to make a random numbrt generator that choses 5 numbers between 1 and 8. The problem i am having is that if the first number is 6 then the sequence will always be 61253, and if it is 1 then the sequence is always 12537, and so on for all 1 to 8. Any sugestions would be appreciated.
    Random numbers with PBP aren't really random, they're pseudo-random.
    If you seed the random number generator with X, you'll always get Y. That's just the way it is. That's what the PBP manual means by 'walking length'. All things being equal, the random number output will repeat itself with the same sequence if you use it exactly the same way every time.
    Add some element of real random-ness, and you might get what you want...i.e. start up one of the timers and pre-seed the random value with the timer value when a button is pushed, that's just one example.

  2. #2
    Join Date
    Dec 2005
    Location
    So Cal and loving it
    Posts
    40


    Did you find this post helpful? Yes | No

    Default

    I place a random statement to the main loop, that way while waiting for a button push you are randomizing in the background.

    Paul
    +-------------------------------------------------------------------+
    | PBP 2.47/2.50 | MCS+ 3.0.0.5 | U2 Prog | Vista x64 | NO SLEEP!!!!!!! |
    +-------------------------------------------------------------------+

  3. #3
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by locko View Post
    I place a random statement to the main loop, that way while waiting for a button push you are randomizing in the background.
    Not bad...not a bad idea at all...pseudo-random - pseudo-randomness...

  4. #4
    Join Date
    Feb 2005
    Location
    Holmfirth England
    Posts
    116


    Did you find this post helpful? Yes | No

    Smile

    Thnx for the replies, as you can see i did have a loop like that right at the beginning of the program

    jmp1:
    random temp
    if sw1 = 1 then goto start
    goto jmp1

    I first tried using a similar loop between each number fetching routine, but this meant pressing the button 5 times.

    In the end i have used 2 pics. The first one uses 5 random vars to pause for random ammounts of micro seconds, and acts as the button press, the second one simply counts up until it sees a button press, then i use //9 to get the number from 0 to 8. checks to see if it is zero or has already been chosen & repeats if so.

    In conclusion, the pbp random statement is the least random thing I have ever come across, a mate tells me he had a similar problem with visual basic, so it is not a flaw with PBP. I just wish the lottery used a computer, I might be able to work out the winning numbers.

    Thanks for the help guys

  5. #5


    Did you find this post helpful? Yes | No

    Default

    Try something like this:
    LET X = 0

    JMP1:
    LET X = (X + 1)
    IF X >=9 THEN LET X = 0
    IF SW1 = 1 THEN START
    GOTO JMP1

    YOU WILL GET A RANDOM NUMBER FROM 0 TO 8. IT WILL BE TRULY RANDOM. IT IS POSSIBLE FOR THE NUMBER TO BE THE SAME WITH A SUBSEQUENT PUSH OF THE SWITCH.

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by peterdeco1 View Post
    Try something like this:
    LET X = 0

    JMP1:
    LET X = (X + 1)
    IF X >=9 THEN LET X = 0
    IF SW1 = 1 THEN START
    GOTO JMP1

    YOU WILL GET A RANDOM NUMBER FROM 0 TO 8. IT WILL BE TRULY RANDOM. IT IS POSSIBLE FOR THE NUMBER TO BE THE SAME WITH A SUBSEQUENT PUSH OF THE SWITCH.
    Not sure it can be called truly random. Given a certain amount of time the number will always be the same.
    Dave
    Always wear safety glasses while programming.

  7. #7
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Dave
    Always wear safety glasses while programming.

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    What I usually do when I need to use RANDOM, is to seed it with a 16 bit timer value, and then add a few other variables I've got around and always tie grabbing that random number with some sort of human interaction.
    If you've got an event that relies on human interaction somehow (i.e. button press) and a tight loop somewhere, there ain't no way you're going to get a continuously repeatable result.
    True, RANDOM in PBP isn't all that random, but when you start adding the 'YOU' factor to it, you can get decent results...

Similar Threads

  1. Questions on random numbers and multiplexing LEDs
    By AaronM in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th June 2009, 04:22
  2. Replies: 4
    Last Post: - 15th April 2009, 01:54
  3. Limite random numbers from 1 to 9
    By Pedro Santos in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 15th March 2007, 12:34
  4. 6 random numbers limiting to 49 ?????
    By SuB-ZeRo in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 15th June 2005, 23:27
  5. random numbers 1-4
    By tekdavid in forum mel PIC BASIC
    Replies: 3
    Last Post: - 30th March 2005, 11:05

Members who have read this thread : 3

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