2 Replies Latest reply on May 9, 2007 3:02 PM by storypeddler

    random numbers to make simple bingo game

    storypeddler
      Hi folks.

      I am making a simple Bingo game in Director.

      1. generate a random number up to 75; Number=random(75)
      2. check if this number is on a list
      3. if not the number is valid and code executed to display the "bingo ball"
      4. add the number to the list
      and repeat.

      Is this valid? Are there problems with this approach, ie. as the numbers are called, the random number generated has more likelihood of being on the list already so will speed take a hit? OR is there another way to do this?

      Thanks in advance
        • 1. random numbers to make simple bingo game
          Chunick Level 3
          The issue with your approach will be evident when you've removed a bunch of numbers already. What you will see is that the number of times you will have to repeat step 2 will grow each time you draw a bing ball as the number of balls drawn increases. How I would approach it is in the reverse...

          1. create your list of numbers from 1 to 75, procedurally at the beginning:

          bingoList = []
          repeat witih j = 1 to 75
          bingoList.add(j)
          end repeat

          -- that gives us a list containing the numbers from 1 to 75.

          2. Now, all you have to do is generate a random number based off the number of items in the list and then remove that item from the list:

          listCount = bingoList.count
          bingoBallPosition = random(listCount)
          -- get the bingo ball value at the position in the list:
          bingoBall = bingoList[bingoBallPosition]
          -- remove that ball number from the list
          bingoList.deleteAt(bingoBallPosition)

          You will have to do other things such as setting the bingoList as a global or property variable depending on how you code your project....

          you may want to se this up in a moviescript if you're a novice programmer so you can call the handlers from anywhere in your project... so you'd probably want to create a newGame handler that would set up your list (and other stuff)....

          -- moviescript

          global bingoList, bingoBall

          on newGame
          bingoList = []
          repeat witih j = 1 to 75
          bingoList.add(j)
          end repeat
          end newGame

          on getBingoBall
          listCount = bingoList.count
          bingoBallPosition = random(listCount)
          -- get the bingo ball value at the position in the list:
          bingoBall = bingoList[bingoBallPosition]
          -- remove that ball number from the list
          bingoList.deleteAt(bingoBall)
          -- do other stuff here such as display the bingo ball
          end getBingoBall
          • 2. Re: random numbers to make simple bingo game
            storypeddler Level 1
            Thanks Chunick. That makes so much more sense than the way I was approaching it!!! My code was ten times longer!!!