Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bingo Example: Design (Random Distinct Numbers Algorithm)

Similar presentations


Presentation on theme: "Bingo Example: Design (Random Distinct Numbers Algorithm)"— Presentation transcript:

1 Bingo Example: Design (Random Distinct Numbers Algorithm)
J. Michael Moore CSCE 121

2 Random Number Generators
Really pseudorandom number generator Given the same seed, it will always produce the same sequence of “random” values If you don’t know the seed, the choices seem random. Can give the same value two times in a row In C++, take remainder to get values in a specified range

3 Goal Fill container with n distinct numbers.
If they didn’t have to be distinct, we’d just use the built in random number generator directly.

4 Build a Model Let’s do one based on how the random number generator works…

5 Brute Force Potential Infinite Loop!
What if you always get the same random number? Possible if not Probable Assume maxVal contains the maximum value numToGet contains the number of values to get Start with empty container, vals While size of vals < numToGet candidate ← randomvalue from 1 to maxVal newVal ← true (assume candidate will work) For each item in vals if item equals candidate newVal ← false (candidate is already there) If newVal add candidate to vals Potential Infinite Loop!

6 Build a Different Model
How do we actually draw numbers?

7 Alternative Idea Fill another container with all of the possible values to use Draw value and put in our container of distinct values gets removed from possible values Since we removed the value, the next time we get one, we won’t get a duplicate!

8 Draw No Infinite Loop! Assume Start with empty container, vals
maxVal contains the maximum value numToGet contains the number of values to get Start with empty container, vals Load bag with values from 1 to maxVal While size of vals < numToGet index ← randomvalue that is any valid index for bag value ← bag[index] Remove value from bag Add value to vals No Infinite Loop!


Download ppt "Bingo Example: Design (Random Distinct Numbers Algorithm)"

Similar presentations


Ads by Google