This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
stashdog's profile picture

Random number generator help

Started by stashdog, 19 April 2014 - 04:30 AM
stashdog #1
Posted 19 April 2014 - 06:30 AM
Hello. I'm trying to create a program that will allow for card games in computercraft.(Why? Well, why not?) But the problem is that I've hit a snag. Well, two of them. First, while I do have a random number generator in my script, I don't know how to create a number generator that will only choose between specific numbers. I.e. between 1, 10, 20,14, etc. instead of say, 1-54. I also have absolutely no idea how to remove numbers from my script once I do have the ability to create the number generator.
Here is a copy of my script: http://pastebin.com/pFG0pYC6 Thank you for any input you might have.
CometWolf #2
Posted 20 April 2014 - 08:50 PM
Oh my, you would have saved so much time by learning tables before doing this :P/> And now you're gonna have to anyways…
http://lua-users.org/wiki/TablesTutorial
You could store your cards in a table, with the card number as the index, and the card name as the value

tCards = {
  [1] = "Ace of Spades"
}
Then when we want a random card from that table, we can just use it's size as the parameters for math.random

local randomCard = table.remove(tCards,math.random(1,#tCards)
The table.remove i threw in there will remove that card from the table aswell as moving the keyes around so we don't create any holes in the table.
Now, we can just print the value stored in the table along with "You drew the "

print("You drew the "..randomCard)
Edited on 20 April 2014 - 06:50 PM