Slide 1

Slide 1 text

Random Number Generators (RNG) Knoxville Game Design August 2019 Levi D. Smith

Slide 2

Slide 2 text

Real World RNG • Dice – integer, 1 to 6 • Coin flip – boolean: heads, tails • Magic Eight Ball – boolean: yes, no (sort of) • Draw a card • Spin a wheel • Rock, Paper Scissors

Slide 3

Slide 3 text

Random Number Tables • Random numbers published in books • Integers • Point at a number • Remove numbers out of range • Remove doubles

Slide 4

Slide 4 text

Quick and Dirty methods • Modulo of system time or frame count for range • Add lower bound • The longer the tick, the less random the value will be • In 6502 NES games • Can increment a variable on every frame • An 8-bit integer rolls over at 256 • Frame variable can be used for a random value between 0 and 256 Random number in bash script

Slide 5

Slide 5 text

Unity / C# Examples Coin Flip Rock, Paper, Scissors Dice Roll

Slide 6

Slide 6 text

Unity / C# - Draw Cards from a Deck • Create List of Card objects (Suit, Rank) • Two approaches • Shuffle List of Cards • Pick card from random position

Slide 7

Slide 7 text

GameMaker GML Examples Dice Roll Coin Flip Rock, Paper, Scissors

Slide 8

Slide 8 text

Unreal Blueprints Examples Dice Roll Coin Flip

Slide 9

Slide 9 text

Seeds • Used for generating the same set of random numbers • Good for testing • Could be used for multiplayer game • Seed is generated on the server and sent to all players • Each player gets the same series of random items

Slide 10

Slide 10 text

Bag method • Seven items are randomized then dealt • After the “bag” is empty, another seven items are randomized and dealt • Prevents runs of same item occurring • Although, an item could appear twice consecutively if it was last in one bag and first in the next bag • https://tetris.fandom.com/wiki/Random_Generator

Slide 11

Slide 11 text

Bag example Completely Random Using Bag Method

Slide 12

Slide 12 text

Bag Method Code Each time Get Piece is clicked, a random number (0 through 6) is drawn from the bag and added to the list If the bag is empty, then it is refilled with integers (0 through 6)

Slide 13

Slide 13 text

Probability • Two dice • Probability of rolling 12 • [6, 6] = 1 outcome • 36 total outcomes • 1 / (6 * 6) = 1 / 36 = 2.78% • Probability of rolling 7 • [1,6] [2,5] [3,4] [4,3] [5, 2] [6, 1] = 6 outcomes • 6 / (6 * 6) = 1 / 6 = 16.67%

Slide 14

Slide 14 text

Combination • How many different combinations of an 8 card hand from a 75 card deck? (each card is unique, order in hand does not matter) • n choose k = n! / k! (n - k)! • 75 choose 8 = 16,781,053,725 • Chance of drawing a specific combination is 1 / 16,781,053,725 or 0.000000006%

Slide 15

Slide 15 text

Permutations • Order does matter • P(n, k) = n! / (n – k)! • How many different sequences of first two pieces in Tetris? (bag method) • P(7, 2) = 7! / (7 – 2)! = 7! / 5! = 5040 / 120 = 42 • or 7*6*5*4*3*2*1 / 5*4*3*2*1 = 7 * 6 = 42 • no bag = 72 = 7 * 7 = 49 • How many different sequences of first five pieces in Tetris? (bag method) • P(7, 5) = 7! / (7 – 5)! = 7! / 2! = 5040 / 2 = 2520 • or 7*6*5*4*3*2*1 / 2*1 = 7 * 6 * 5 * 4 * 3 = 2,520 • no bag = 75 = 7 * 7 * 7 * 7 * 7 = 16,807