Jump to content




Random Choice


7 replies to this topic

#1 jtdavis99

  • New Members
  • 39 posts

Posted 02 March 2012 - 08:18 PM

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 Liraal

  • New Members
  • 477 posts
  • LocationPoland

Posted 02 March 2012 - 08:21 PM

math.random(min, max)

#3 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 02 March 2012 - 08:22 PM

Yes, there is. You'll want to use a table though:

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.


#4 jtdavis99

  • New Members
  • 39 posts

Posted 02 March 2012 - 08:22 PM

View PostLiraal, on 02 March 2012 - 08:21 PM, said:

math.random(min, max)
Is there a way to do it with words?

#5 ChaosBeing

  • New Members
  • 18 posts
  • LocationMy Computer. Possibly, my computer inside my computer.

Posted 03 March 2012 - 06:58 AM

That's what the code above does. See this line of Advert's example?
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 Glaze

  • Members
  • 3 posts

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 :D
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 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 29 September 2013 - 07:13 PM

You can't have a variable called 8.

#8 Glaze

  • Members
  • 3 posts

Posted 06 October 2013 - 10:20 AM

View Postimmibis, on 29 September 2013 - 07:13 PM, said:

You can't have a variable called 8.
F i derped sorry





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users