1

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.

1
  • after 3 hours of self riddling, I got it working! no worries! Commented Dec 27, 2020 at 23:57

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

3 Comments

math.randomseed(os.clock() * 10000) fails with "bad argument #1 to 'randomseed' (number has no integer representation)" in Lua 5.4.4 on Linux.
math.randomseed(os.clock()) should work. 5.4 and it's darn ints :/ @DaveJarvis
Maybe revise your answer to help others who don't scroll down to see the comments?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.