A Simple Guide to Randomization. (Position, Tables, Values)

This is my first tutorial so it might not be the best.

Before we begin, I’m going to list some things you might want and specifically how you format/use them.
Quick Notice: math.random doesn’t have to be used, however I will be using it for this tutorial. Here is another possibility A Random Feature

math.random() -- math.random will select a random number between the two numbers in the parenthesis. The smallest number should be first. There doesn't have to be anything between the parenthesis, however I recommend it. Example;

math.random(1, 10)

Tables -- tables are used to store different values/things. In tables, there are different positions depending on how many things are in your table. For example, in this table;

local numTable = {3, 2, 1} -- the numbers in the table don't matter, but the positions do. To grab 3 from the table, I would do:

numTable[1] -- 1 is the position of 3 in the table. Position 2 would be the number "2", and Position 3 would be The number 1.

table.remove() -- removing items from tables. With my table, I would do it like this:

table.remove(numTable, 1) -- this would remove the value that takes up Position 1 in the table. In this case, "table.remove(numTable" refers to the table I want to remove the value from, and "table.remove(numTable, 1)" refers to the number position of the value being removed.

table.insert -- adding things to a table.
local Number = 1
local numTable = {1, 2, 3}
table.insert(numTable, Number) -- table.insert(numTable) refers to the table we are adding it to. table.insert(numTable, Number) refers to the value/thing we are adding to the table.

table.insert(numTable, Number, 1) -- the "1" refers to which position I want it to be in. This is optional.

Vector3.new() -- this is a way of doing 3 numbers for something like, Position, Orientation, and Size.

To start, lets randomize numbers. To make a simple random number generator, we would use print and math.random. Let’s make it randomize a number between 1 and 10 every 10 seconds.

    while true do
    wait(10)
    local RandomNum = math.random(1, 10)
        print(RandomNum)
end

Every 10 seconds, it will print a random number between 1 and 10. Let’s move on!

Randomizing things in tables:
To randomize something in a table, we need to make a table. In this example, I will be doing a table of strings. To start, do the following:

local String1 = "Hello!"
local String2 = "Hey!"
local String3 = "Hi!"
local StringTable = {String1, String2, String3}

This will be our table of strings. To randomize the items in the strings, lets create a function for simplicity:

function StringSelector()
        local StringChosen = StringTable[math.random(1, #StringTable)]

Before we go any further, I will break this down. StringChosen is a variable for a random string that will be chosen. To Randomize the output from the table, I would say the table we are randomizing from, and in the brackets we choose which position from the table we want. In this case, since it is random, we put “math.random” and then in the parenthesis, the things I want to be chosen. In this case, I did “1, #StringTable” which means, the lowest number to be selected is 1. “#StringTable” essentially means, the number of things in this table. To print a random String, we would just add a “print(StringChosen)”.
Alright, that sums up the table section.

Vector3 Randomization:
Let’s say we were making a thing that spawns coins in a park. Instead of making a table full of possible positions, I would make a function that gives the coin a randomized Vector3 inside the park. For example, if my parks edges were (X - 10, 100) | (Y - 5) | (Z - 50, 80) I would do the following:

local Coin1 = game.Workspace.Coin1
local Coin2 = game.Workspace.Coin2
local Coin3 = game.Workspace.Coin3
local CoinTable = {Coin1, Coin2, Coin3}

    function CoinPlace()
        local PlaceChosen = Vector3.new(math.random(10, 100), 5, math.random(50, 80)) -- random place that fits inside the park
        local CoinChosen = CoinTable[math.random(1, #CoinTable)] -- Instead of (1, #CoinTable) you could also just use (#CoinTable)
            while wait(15) do
            CoinChosen.Position = PlaceChosen
end
CoinPlace()

Essentially what this would do, is pick a random place inside the park, pick a random coin, and make that coin’s position a random place in the park.

Alright! This concludes my tutorial on Randomization. Please leave me any feedback and thanks for reading! Stay safe! Small thanks to Operatik for clarifying some things.

16 Likes

Psst. A few number of things to improve here:

math.random()'s second argument is optional. If there’s no second argument, then it will assume the first argument as the maximum, setting 1 as minimum. For instance, math.random(1, #CoinTable) is rewritten to math.random(#CoinTable).

Also there’s a different random API, which doesn’t use math library:

Mind if you cover these?

3 Likes

Done! Thanks for the feedback, and I added a little credit for you.

3 Likes

the smaller number needs to be first. it will error otherwise
and you need to specify that having arguments will cause math.random to return only whole numbers. it will return numbers like 1.25 and 1.00009


you’ve swapped the order of the arguments: position and value


it should be (table, position, value), not (table, value, position)


you should also avoid throwing all of that into a codeblock because it’s more like notes than code
and make sure you explain what happens when users leave things out wherever you say “this is optional”


edit:
oops, someone just sent me your post, so I thought it was recent
just saw that it was 3 years ago

1 Like