Math.random()
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Math.random()
Is there are way to receive a random number WITH a decimal between 2 numbers? Example: math.random(1, 10) = 5.381298317
Check out my latest game: http://love2d.org/forums/viewtopic.php?f=5&t=33349
- slime
- Solid Snayke
- Posts: 3144
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Math.random()
This will give you a number between 1 and 10:
Code: Select all
1 + (math.random() * 9)
Re: Math.random()
Can't you just do this?slime wrote:This will give you a number between 1 and 10:Code: Select all
1 + (math.random() * 9)
Code: Select all
math.random()*10
Check out my latest game: http://love2d.org/forums/viewtopic.php?f=5&t=33349
- slime
- Solid Snayke
- Posts: 3144
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Math.random()
You can, but it'll give you a number in the range of [0, 10] instead of [1, 10] (the latter being what your example asked for.)
- slime
- Solid Snayke
- Posts: 3144
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Math.random()
You could make a function out of it, as well:
Code: Select all
function newRandom(min, max)
return min + math.random() * (max - min)
end
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Math.random()
Hmm... guys, according to the lua-users wiki,
- 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.
When I write def I mean function.
Re: Math.random()
That's what all their code samples use as well, though? Unless you think there's a problem?
Re: Math.random()
You could do
too, but there's rarely a point in that.
Code: Select all
math.random(max-1) + math.random()
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
Who is online
Users browsing this forum: No registered users and 4 guests