Shop OBEX P1 Docs P2 Docs Learn Events
Trouble generating Random Numbers... — Parallax Forums

Trouble generating Random Numbers...

CreamPuffCreamPuff Posts: 1
edited 2007-04-04 03:15 in BASIC Stamp
Hi I am working on a slot machine program that will utilize 4 random numbers and display them in the Debug terminal...getting·random numbers seem to be a real challenge since all i know is what is contained in the "Whats a MicroController" handbook and in the help index...which really didnt help much. I also need to figure out how to make the numbers cycle in the debug terminal...to·imitate a real slot machine...thanks for any help you can give...yeah.gif

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-04-04 03:15
    Hi, the RANDOM function returns a series of pseudo random numbers, that series will always be the same series of numbers if you give RANDOM the same "seed" each time.

    So firstly you need to vary the seed before you call the random statement. One way is to increment a number in a loop, when you jump out of the loop some number is passed to the random statement which returns your random number series. To increase the possible variation in the "seed" ·number I used a word variable that counts to 65535 and back to zero·and continually·repeats until an input (IN0) is taken high which breaks the loop.

    To derive the range of numbers I have used the modulo· seed // 10 and added 1 which in the example gives a range of 1 to 10

    seed VAR Word
    rnd VAR Word

    main:
    · DO WHILE IN0<1
    ··· seed=seed+1
    · IF seed >65535 THEN
    ··· seed=0
    · ENDIF
    · LOOP
    · RANDOM seed
    · rnd=seed//10+1
    · DEBUG DEC rnd," "
    GOTO main

    Jeff T.
Sign In or Register to comment.