lbenson
Senior Member
Pick numbers randomly from a list without duplicates (or deal a deck of cards)
A quesition was posed about how to randomly pick each number from a list with no duplicates until each is chosen. The following program does this. To get a random number to start the sequence of random number used in the choosing loop, the program starts with the value of the "timer" register, which is available only on X1 and higher parts. The data goes in the eeprom starting at position 0, and may be put there with the EEPROM or DATA statement, or by the program (e.g., the numbers 1-52 representing the cards in a deck).
The program loops backwards by minus 1 from the number of items down to 2. A "slot" or position for the next chosen item is picked using the remainder operator ("//"). The value at this slot is displayed as the chosen value. The number in the last available slot is then moved to replace the chosen number (which may result in no change if the last slot has been chosen), and the loop is then repeated with one less slot available to chose from. When the loop is done, the last unchosen number, is displayed from slot 0.
For other than X1 parts, a different method must be chosen to get the starting random number value.
A quesition was posed about how to randomly pick each number from a list with no duplicates until each is chosen. The following program does this. To get a random number to start the sequence of random number used in the choosing loop, the program starts with the value of the "timer" register, which is available only on X1 and higher parts. The data goes in the eeprom starting at position 0, and may be put there with the EEPROM or DATA statement, or by the program (e.g., the numbers 1-52 representing the cards in a deck).
The program loops backwards by minus 1 from the number of items down to 2. A "slot" or position for the next chosen item is picked using the remainder operator ("//"). The value at this slot is displayed as the chosen value. The number in the last available slot is then moved to replace the chosen number (which may result in no change if the last slot has been chosen), and the loop is then repeated with one less slot available to chose from. When the loop is done, the last unchosen number, is displayed from slot 0.
For other than X1 parts, a different method must be chosen to get the starting random number value.
Code:
' 28Random.bas -- randomly pick each number from a list
' with no duplicates until each is chosen
#picaxe 28x1
symbol nItems = 8
symbol lastValue = b7 ' value in last slot
symbol lastSlot = b8 ' last position in current unchosen list
symbol loopCount = b9 ' counter for looping
symbol value = b10 ' the chosen value
symbol slotChoice = b11 ' the chosen position
symbol randomNo = w6 ' random number
' put nItems of data starting at eeprom 0 (by any method)
eeprom 0,(0,1,2,3,4,5,6,7)
' seed random number with "timer" register (available on X1 parts)
randomNo = timer ' will vary if embedded in larger program somewhere
for loopCount = nItems to 2 step -1 ' loop, counting down
random randomNo ' get new random number
lastSlot = loopCount - 1 ' last position of shrinking array
slotChoice = randomNo // loopCount ' position chosen
read slotChoice,value ' value at position chosen
sertxd (#value) ' display chosen value
read lastSlot,lastValue ' take highest remaining value
write slotChoice,lastValue ' put in slot for used value
write lastSlot, value ' not needed, but retains the chosen list (backwards)
next loopCount
read 0,value ' last unchosen value now in position 0
sertxd (#value) ' display last value
Last edited: