Win the Lottery (Reprise)

Previous pageNext page

"Generate another winning lottery ticket when the Newsagent points out the duplicates in the above (display the result of evaluating next 4 lines with Ctrl+D)"

r := Random new.

s := Set new.

[s size = 6] whileFalse: [s add: (r next * 49) rounded+1].

s asSortedCollection asArray.

 


Okay, so you were humbled when the newsagent informed you that your first "lottery winning" program didn't always generate valid tickets. Some of the numbers were duplicated! However, if you select all of the lines in the above, somewhat more complex, expression and use Display It, you should now get a valid ticket.

This experiment uses the, rather useful, Smalltalk Set collection class to avoid getting duplicates. We just have to write a loop that goes around  adding the random ticket numbers to a set until we get six unique values.

Remember, to get back to these experiments just choose Help/First Splash in any Dolphin tool window.

 

.