Faw[Ful] Posted August 13, 2010 Share Posted August 13, 2010 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
madis Posted August 13, 2010 Share Posted August 13, 2010 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now