Aloan Moreira
Aloan Moreira

Reputation: 9

How do I use a random number?

1

I have tried 2 methods and they are both in the screenshot. How do I use random? I would be so happy if someone solves this one for me.

Upvotes: 1

Views: 736

Answers (1)

Mooshua
Mooshua

Reputation: 539

Here, we are running into several problems with lua syntax and it's internals. First, let's understand how random works.

#1. The Random Problem.


When you get a random number from lua, it's not really random. This is because true randomness is practically impossible; It is a lot more effort than it is worth.

For this reason, Lua (and many other languages) have "cheated"- they instead use very large numbers to calculate the result of random.

Disclaimer: I do not know how Lua's random function works. This is a generic random function that is in the same caliber as Lua's.


To start with, we need a truly random number. For this, we use randomseed. Whenever we call randomseed, the random state is completely erased.

Consider the following code:

print(math.random(1,4))
print(math.random(1,4))
print(math.random(1,4))

Run this code. Run it a lot. No matter how many times you run it, it will always be the same four numbers. This is because lua's random function is just math, it's not really random.

Let's look at this: We start with a random seed, the current time.

math.randomseed(os.clock()*100000)
print(math.random(1,4))
print(math.random(1,4))
print(math.random(1,4))

Every time you run this, the numbers will be unique. This is because we take the current time as the seed, so the random is always in a scrambled state. Likewise,

math.randomseed(2)
print(math.random(1,4))

This code will always output 3.

To solve this, let's change your code a little bit.

math.randomseed(os.clock() * 10000)
function Behavior:Awake()
    self.timer = 0
    place = math.random(1,4)
end

This should work, because it does not consistently reset the state. If it does not- let me know and I shall update with another solution.

#2. Lua Variables


You displayed some initial code:

function Behavior:Awake()
    self.Timer = 0
    place = 1, 4
end

This does not generate a random value. It instead follows some lesser-known lua syntax. Consider the following code:

local a,b = "a","b"

This demonstrates the code rule. If you haven't guessed it by now, lua assigns variables sequentially. When you define a variable:

local a

You may provide any amount of values to fill this variable (separated by , of course). However, Lua only chooses the first one.

local a = 1,2,3,4,"and to infinity!"
print(a) --> 1

You can do the same for variables:

local first, second, last = 1,2,3,4,5,6,"and beyond..."
print(first,second,last) --> 1, 2, 3

This is useful for multiple reasons. For example, you can return multiple values from a single function:

function awesome(a)
    return (a == "sauce"), ("awesome "..a)
end
local isAwesome, Text = awesome("sauce")
print(isAwesome, Text) --> true, "awesome sauce"

...You can swap two variables around without needing a third one...

local a,b = b,a

This is also used a lot in pcall, which catches errors (so if part of your code errors- you can tell, and stop it)

function IWillError()
    error("told you so...")
end

local Success,Fail = pcall(IWillError)
print(Success, Fail) --> false (the code errored), "told you so..." (the error)

Upvotes: 3

Related Questions