I was wondering if there was a way to randomly pick one thing from a bucket of others? So, like random("red, blue, green, purple") and it'd pick one out of that, as an example.
2
Random Choice
Started by jtdavis99, Mar 02 2012 08:18 PM
7 replies to this topic
#1
Posted 02 March 2012 - 08:18 PM
#2
Posted 02 March 2012 - 08:21 PM
math.random(min, max)
#3
Posted 02 March 2012 - 08:22 PM
Yes, there is. You'll want to use a table though:
The function you're looking for is math.random(min, max), and you'll want to use it in addition to a table, and it's # operator (returns the total number of items in it).
Note that the # operator will only return the count of items starting from 1, until it doesn't exist:
local items = {"apple", "pie", "banana", "orange"} print(items[math.random(1, #items)]) -- Pick a random item -- Do it again a few times: for i = 1, 10 do print(items[math.random(1, #items)]) end
The function you're looking for is math.random(min, max), and you'll want to use it in addition to a table, and it's # operator (returns the total number of items in it).
Note that the # operator will only return the count of items starting from 1, until it doesn't exist:
local items = {"apple", "pie", 4 = "banana"} print(#items) -- This will print 2, since items[3] is nil.
#5
Posted 03 March 2012 - 06:58 AM
That's what the code above does. See this line of Advert's example?
That's creating a table (think of table's as a kind of list) of the words "apple", "pie", "banana", and "orange".
Then this,
Prints one of them out at random. He uses math.random to get a random number between 1 and the number of items in the table, and then uses that number to access that word. Using the above example again, there's four words for it to choose from, so random will select any number from 1 to 4.
Now, see how the math.random is inside of square brackets? These square brackets tell the table 'items' which word you're looking for. For example, if math.random returned 3, that line would print out "banana". Just the same as how if math.random return 2, it'd print out "pie".
local items = {"apple", "pie", "banana", "orange"}
That's creating a table (think of table's as a kind of list) of the words "apple", "pie", "banana", and "orange".
Then this,
print(items[math.random(1, #items)]) -- Pick a random item
Prints one of them out at random. He uses math.random to get a random number between 1 and the number of items in the table, and then uses that number to access that word. Using the above example again, there's four words for it to choose from, so random will select any number from 1 to 4.
Now, see how the math.random is inside of square brackets? These square brackets tell the table 'items' which word you're looking for. For example, if math.random returned 3, that line would print out "banana". Just the same as how if math.random return 2, it'd print out "pie".
#6
Posted 29 September 2013 - 01:21 PM
ChaosBeing, I want to implement this in a script for a survival map so it would randomly pick an option and my script is for wired networks
My script
peripheral.wrap("computer_0")
peripheral.wrap("monitor_3")
peripheral.call("bottom", "callRemote", "monitor_3", "write", " Hack Succesfull")
sleep(15)
local items = {"1", "2", "3"}
8 = items[math.random(1, #items)] -- Random
if 8 == 1 then do
local function OS()
shell.run("startup")
end
else
2 = local function OS2()
shell.run("server.terminated")
end
end
parallel.waitForAny( OS, OS2 )
Everything works exept the random chooser
My script
peripheral.wrap("computer_0")
peripheral.wrap("monitor_3")
peripheral.call("bottom", "callRemote", "monitor_3", "write", " Hack Succesfull")
sleep(15)
local items = {"1", "2", "3"}
8 = items[math.random(1, #items)] -- Random
if 8 == 1 then do
local function OS()
shell.run("startup")
end
else
2 = local function OS2()
shell.run("server.terminated")
end
end
parallel.waitForAny( OS, OS2 )
Everything works exept the random chooser
#7
Posted 29 September 2013 - 07:13 PM
You can't have a variable called 8.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users