Jump to content

math random question


Faw[Ful]

Recommended Posts

Hi there, I can use math random to make a random choice between two values like math.random (50, 55) and its include 51 52 53 and 54 , and exclude all the other,

but can I set it to make a choice randomly just for the two values so its 20 or 25 but not between the two so its exclude values 21 22 23 and 24 and all the other.

Can I do that ?

Link to comment

math.random itself doesn't offer such functionality

math.random() with no arguments generates a real number between 0 and 1.

math.random(upper) generates integer numbers between 1 and upper.

math.random(lower, upper) generates integer numbers between lower and upper.

However, it's easy to make a function for that. This takes in array-type table and returns random element from it.

function getRandomValue(acceptedValues)
if acceptedValues == nil or #acceptedValues == 0 then
return false
end
 randomIndex = math.random(#acceptedValues)
return acceptedValues[randomIndex]
end

Example usage:

getRandomValue({20,25})

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...