Page 1 of 1

Generating random float variables

Posted: Tue Jul 31, 2012 7:57 am
by YGOFreak1997
Hello Guys!

I would like to know how to generate random float variables (just like the title says ^^). But not just that, i want to be able to choose how much decimal places it has. Is that possible in lua/LÖVE? I already have a litte piece of code to do this, just like:

Code: Select all

shootingRate = math.random() + math.random(1.2, 2.5)
But in the first "math.random", i can't choose how much decimal places it generates :/

Thanks for your help!

Re: Generating random float variables

Posted: Tue Jul 31, 2012 8:29 am
by Santos
I found this: http://devwiki.metaplace.com/wiki/index ... in_a_range
And this: http://ardoris.wordpress.com/2008/11/07 ... es-in-lua/

So, this should work. I think. :ultraglee:

Code: Select all

function random(min, max, precision)
	local precision = precision or 0
	local num = math.random()
	local range = math.abs(max - min)
	local offset = range * num
	local randomnum = min + offset
	return math.floor(randomnum * math.pow(10, precision) + 0.5) / math.pow(10, precision)
end
EDIT: Here's a slightly different version (when precision is nil, it doesn't round the random number at all):

Code: Select all

function randomFloat(min, max, precision)
	-- Generate a random floating point number between min and max
	--[[1]] local range = max - min
	--[[2]] local offset = range * math.random()
	--[[3]] local unrounded = min + offset

	-- Return unrounded number if precision isn't given
	if not precision then
		return unrounded
	end

	-- Round number to precision and return
	--[[1]] local powerOfTen = 10 ^ precision
	local n
	--[[2]] n = unrounded * powerOfTen
	--[[3]] n = n + 0.5
	--[[4]] n = math.floor(n)
	--[[5]] n = n / powerOfTen
	return n
end
Example run-through:

randomFloat(1, 10, 3)

1: range = max - min
10 - 1
9

2: offset = range * math.random()
9 * (0.0 to 1.0)
(0.0 to 9.0)

3: unrounded = min + offset
1 + (0.0 to 9.0)
(1.0 to 10.0)

Example unrounded number: 1.23456789
Rounded to three decimal places: 1.235

1: 10 ^ precision (3 in this example)
1000

2: 1.23456789 * 1000
1234.56789

3: 1234.56789 + 0.5
1235.0678

4: Floor of 1235.06789
1235

5: 1234 / 1000
1.235


Written more concisely:

Code: Select all

function randomFloat(min, max, precision)
	local range = max - min
	local offset = range * math.random()
	local unrounded = min + offset

	if not precision then
		return unrounded
	end

	local powerOfTen = 10 ^ precision
	return math.floor(unrounded * powerOfTen + 0.5) / powerOfTen
end

Re: Generating random float variables

Posted: Wed Aug 01, 2012 4:57 am
by luther
YGOFreak1997 wrote:

Code: Select all

shootingRate = math.random() + math.random(1.2, 2.5)
I'm not exactly sure what you're trying to do with this. According to the Lua manual, math.random expects integer arguments, so the second call would only return 1 or 2.

IMO, if you must round off, you should do it as the last step of the calculation. In other words, there's no need to try to constrain math.random. Here's a general rounding-off function:

Code: Select all

function roundOff(x, precision)
   --Make a format string with the right precision
   local formatString = ('%%.%df'):format(precision)
   --Convert x to a rounded off string, then back to a number
   return tonumber(formatString:format(x))
end
Santos's idea of multiplying by a power of 10 can also work.

Re: Generating random float variables

Posted: Sun Aug 05, 2012 1:10 pm
by Robin
Since the exact value 0.1 is infinitely long in binary, it matters what you want to do. If rounding errors are acceptable, the above solutions work. If they aren't, you need to multiply the numbers by a power of 10 and keep them that way. For example, if you're working with dollars and cents, instead of making 1.0 a dollar and 0.01 a cent, use 100 for a dollar and 1 for a cent. There are no rounding errors when you use numbers like that.

Re: Generating random float variables

Posted: Mon Aug 06, 2012 4:49 pm
by YGOFreak1997
luther wrote:
YGOFreak1997 wrote:

Code: Select all

shootingRate = math.random() + math.random(1, 5)
I'm not exactly sure what you're trying to do with this. According to the Lua manual, math.random expects integer arguments, so the second call would only return 1 or 2.

With the first "math.random", i create a random float between 0 and 1 and with the second one, i add a random interger to the first amount and this way i get a random interger plus the first random float and this is a radom float, isn't it?

Re: Generating random float variables

Posted: Mon Aug 06, 2012 5:04 pm
by Nixola
If the first number is 1 you can omit it (math.random(5))

Re: Generating random float variables

Posted: Mon Aug 06, 2012 9:20 pm
by YGOFreak1997
Okay, that's right but that was just an example ^^ Anyway, my code works too, doesn't it?

Re: Generating random float variables

Posted: Thu Mar 21, 2013 11:42 pm
by luther
YGOFreak1997 wrote:
luther wrote:
YGOFreak1997 wrote:

Code: Select all

shootingRate = math.random() + math.random(1, 5)
With the first "math.random", i create a random float between 0 and 1 and with the second one, i add a random interger to the first amount and this way i get a random interger plus the first random float and this is a radom float, isn't it?
A simplified version:

Code: Select all

shootingRate = math.random() * 5 + 1