Shop OBEX P1 Docs P2 Docs Learn Events
True Random Numbers Generator — Parallax Forums

True Random Numbers Generator

Regarding my post dated 2022-09-23 "Random Seed" below is the program from Jon McPhalen with some modifications added previously. This program runs a set of 6 Random Numbers generating (up to 70) with external trigger/seed using a 10k resistor , 10UF cap to pin 15 , also using a N.O. momentary switch from pin 15 to ground. Pressing the switch 6 times will display 6 random numbers. Continuously pressing the switch 6 times will display another 6 random numbers. Thanks Jon.

' =========================================================================
'
' File...... jm_lotto_demo.bs2
' Purpose...
' Author.... Jon McPhalen
' Copyright (c) 2022 Jon McPhalen
' MIT License Applies
' E-mail....
' Started...
' Updated... 25 SEP 2022
'
' {$STAMP BS2}
' {$PBASIC 2.5}
'Lotto numbers generator
' =========================================================================

' -----[ Program Description ]---------------------------------------------
'connect 10k pulldown and 10uf to pin 15
'connect N.O. switch from PIN 15 to gnd

' -----[ Revision History ]------------------------------------------------

' -----[ I/O Definitions ]-------------------------------------------------

Trigger PIN 15 ' for trigger input

' -----[ Constants ]-------------------------------------------------------

IS_ON CON 1 ' for active-high in/out
IS_OFF CON 0

R_MIN CON 1 ' for random #s
R_MAX CON 70
R_SPAN CON R_MAX - R_MIN + 1

' -----[ Variables ]-------------------------------------------------------

ballCnt VAR Nib ' current state of draws
check VAR Nib ' loop index

timer VAR Byte ' for trigger
ball VAR Byte(6) ' balls drawn

lottery VAR Word ' random value

' -----[ Initialization ]--------------------------------------------------

Reset:
DEBUG CLS, "Lottery Demo", CR
IF (Trigger = IS_ON) THEN
DEBUG "-- Draw button is pressed."
PAUSE 1000
DO WHILE (Trigger = IS_ON)
' wait for button to clear
LOOP
GOTO Reset
ENDIF

New_Game:
ballCnt = 0
DEBUG CR, "Draw: "

' -----[ Program Code ]----------------------------------------------------

Main:
timer = 0 ' reset timer
DO
RANDOM lottery ' stir random value
PAUSE 5
timer = (timer + 5) * Trigger ' re-scan trigger input
LOOP WHILE (timer < 25) ' wait for valid trigger

Draw:
RANDOM lottery
ball(ballCnt) = lottery // R_SPAN + R_MIN ' create ball
FOR check = 0 TO 5 ' validate
IF check < ballCnt THEN
IF ball(ballCnt) = ball(check) THEN ' if repeat
GOTO Draw ' try again
ENDIF
ENDIF
NEXT

DEBUG DEC2 ball(ballCnt), " " ' display new ball
ballCnt = ballCnt + 1 ' update count
IF ballCnt < 6 THEN ' if not done
PAUSE 250 ' wait a bit
DO WHILE (Trigger = IS_ON) ' force button release
' hold
LOOP
GOTO Main
ENDIF

PAUSE 2000

GOTO New_Game

' -----[ Subroutines ]-----------------------------------------------------

' -------------------------------------------------------------------------

' -------------------------------------------------------------------------

' -------------------------------------------------------------------------

' -------------------------------------------------------------------------

' -------------------------------------------------------------------------

' -----[ User Data ]-------------------------------------------------------

Comments

  • 1) Use the code formatting tool in the forums toolbar. Drag-select your code, then click on the paragraph symbol to get a drop-down. Select 'code' from the list.

    2) If you change something I wrote, please take my name off it so I'm not credited for your work.

  • Ok Jon. I don't know how to take your name from this post.

Sign In or Register to comment.