Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Random Number Generators (RNG)

LD Smith
August 11, 2019

Random Number Generators (RNG)

Presentation video - https://www.youtube.com/watch?v=MUeyXKqzJrY
This month’s Knoxville Game Design topic is Random Number Generators (RNG). We will discuss the various methods for generating random values in game development. We will also look at combinatorics for determining the likelihood of outcomes.

LD Smith

August 11, 2019
Tweet

More Decks by LD Smith

Other Decks in Technology

Transcript

  1. 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
  2. Random Number Tables • Random numbers published in books •

    Integers • Point at a number • Remove numbers out of range • Remove doubles
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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)
  8. 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%
  9. 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%
  10. 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