Generating random float variables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
YGOFreak1997
Party member
Posts: 103
Joined: Sun Jan 22, 2012 4:29 pm
Location: Germany, Baden-Württemberg
Contact:

Generating random float variables

Post 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!
Also look at my LÖVE-Tutorial-Youtube-Channel (German ^^)
GitHub Repo: Click Me!
Website: No, Me!
IndieDB: Or Maybe Me?

ÖBEY!
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Generating random float variables

Post 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
luther
Prole
Posts: 3
Joined: Fri Jul 27, 2012 7:59 am

Re: Generating random float variables

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Generating random float variables

Post 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.
Help us help you: attach a .love.
User avatar
YGOFreak1997
Party member
Posts: 103
Joined: Sun Jan 22, 2012 4:29 pm
Location: Germany, Baden-Württemberg
Contact:

Re: Generating random float variables

Post 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?
Also look at my LÖVE-Tutorial-Youtube-Channel (German ^^)
GitHub Repo: Click Me!
Website: No, Me!
IndieDB: Or Maybe Me?

ÖBEY!
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Generating random float variables

Post by Nixola »

If the first number is 1 you can omit it (math.random(5))
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
YGOFreak1997
Party member
Posts: 103
Joined: Sun Jan 22, 2012 4:29 pm
Location: Germany, Baden-Württemberg
Contact:

Re: Generating random float variables

Post by YGOFreak1997 »

Okay, that's right but that was just an example ^^ Anyway, my code works too, doesn't it?
Also look at my LÖVE-Tutorial-Youtube-Channel (German ^^)
GitHub Repo: Click Me!
Website: No, Me!
IndieDB: Or Maybe Me?

ÖBEY!
luther
Prole
Posts: 3
Joined: Fri Jul 27, 2012 7:59 am

Re: Generating random float variables

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests