How to generate a random decimal number in love2D? - lua

I tried generating a random decimal number between (lets say between 0.3 and 0.8) in love2d using the following code:
x=math.random(0.3, 0.8)
print(x)
but what happens is it generates 0.3 every single time I run the program and the 0 in 0.4 kind of flickers (in the sense like it changes to 1).
If it helps, here's a screen record of what happens https://vimeo.com/632949687

Your problem is underspecified. Here are two simple solutions; they're not equivalent.
This generates random numbers in the set {0.3,0.4,0.5,0.6,0.7,0.8}:
math.random(3,8)/10
This generates random numbers in the interval [0.3,0.8):
0.3+(0.8-0.3)*math.random()

In LÖVE there is a platform independent version of random() present.
https://love2d.org/wiki/love.math.random
With no need to use of math.randomseed() or love.math.setRandomSeed().
For float numbers in the range 0 and 1 simply use...
love.math.random()
'but what happens is it generates 0.3 every single time'
Same here, so the simpliest way seems to be #lhf' example.

check the function
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

Related

Rails / Ruby how to always show decimal precision

I am currently converting some python statistics library that needs to produce a number with high decimal precision. For example, I did this:
i = 1
n = 151
sum = (i - 3/8) / (n + 1/4)
it will result to 0.
My question is how to always show decimal precision automatically when I do this kind of computation?
My desired output is:
0.004132231404958678
In ruby all the arithmetic operations result in the value of the same type as operands (the one having better precision.)
That said, 3/4 is an integer division, resulting in 0.
To make your example working, you are to ensure you are not losing precision anywhere:
i = 1.0
n = 151.0
sum = (i - 3.0/8) / (n + 1/4.0)
Please note, that as in most (if not all) languages, Float is tainted:
0.1 + 0.2 #⇒ 0.30000000000000004
If you need an exact value, you might use BigDecimal or Rational.

Lua decimal precision loss

Can someone explain why in lua running:
return 256.65 * 1000000 + .000000005 - 256 * 1000000 gives 649999.99999997
whereas
return 255.65 * 1000000 + .000000005 - 255 * 1000000 and
return 268.65 * 1000000 + .000000005 - 268 * 1000000 give 650000.0 ?
From what i can see it seems to be an issue strictly for decimal 65 (and it seems also 15) and for whole numbers within the range 256 - 267. I know this is related to doing these calculations with floating points, but I'm still curious as to what is special about these values in particular
What is special about these values is that 0.65 is not a binary fraction (even though it is a decimal fraction), and so cannot be represented exactly in floating point.
For the record, this is not specific to Lua. The same thing will happen in C.
For the same reason that 10/3 is a repeating fraction in base 10. In base 3, dividing by 3 would result in whole numbers. In base 2 -- which is used to represent numbers in a computer -- the numbers you're producing similarly result in fractions that can be exactly represented.
Further reading.

Generate a Random Number between 0.0001 and 0.002 in Objective C (iOS)?

Does anyone know how I could generate a random number in a range in iOS? I am currently working on a synthesizer in iOS (using SpriteKit and AudioKit) and I am trying to modify the loudness of the synth with changing its variability whenever a slider is being moved as well.
This is what my code looks like:
[Synth setAmplitude: 0.5 + (slider.currentValue * loudnessVar)];
where 0.5 is the default amplitude value and loudnessVar is a random number.
Since, the slider returns values from -170 to 170 , I would need a relatively low number in order to set a value between 0 and 1.
Is anyone able to help with this?
The way to generate a random number in a range is:
NSInteger random = min + arc4random() % (max - min);
So, you can generate a number between 1-20 and divide it by 1000, it's just an example.
I.

Rails: Math not computing correctly. It's off by .00000000000001

My rails app is not doing math correctly. I think this has something to do with the variable types (int vs float) but not sure what's wrong.
The root problem is this method in my Stat model:
def lean_mass
self.weight * 0.01 * (100 - self.body_fat)
end
Where
Stat.weight = 140
Stat.body_fat = 15
it returns 119.00000000000001 instead of 119.
However, where
Stat.weight = 210
Stat.body_fat = 15
it returns 178.5, the correct number.
Anyone know why it's throwing in that small decimal?
The datatype for weight is integer and body_fat is decimal if that helps.
Floating-point numbers cannot precisely represent all real numbers. And furthermore floating-point operations cannot precisely represent every arithmetic operation. This leads to many surprising situations.
A simple example that shows this behavior:
0.1 + 0.2
#=> 0.30000000000000004
I advise to read: https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
You can avoid most of this problems by using BigDecimal instead of floats:
require 'bigdecimal'
BigDecimal.new('0.01') * 140 * (100 - 15)
#=> 119.0
Take a look at ruby BigDecimal
http://www.ruby-doc.org/stdlib-2.1.1/libdoc/bigdecimal/rdoc/BigDecimal.html
For example, try:
sum = 0
10_000.times do
sum = sum + 0.0001
end
print sum #=> 0.9999999999999062
and contrast with the output from:
require 'bigdecimal'
sum = BigDecimal.new("0")
10_000.times do
sum = sum + BigDecimal.new("0.0001")
end
print sum #=> 0.1E1

Math.random on non whole numbers

How can I generate numbers that are less than 1?
for example i would like to generate numbers from 0.1 to 0.9
what I've tried:
math.random(0.1,0.9)
Lua's math.random() with two arguments returns an integer within the specified range.
When called with no arguments, it returns a pseudo-random real number in between 0.0 and 1.0.
To get real numbers in a specified range, you need to do your own scaling; for example:
math.random() * 0.8 + 0.1
will give you a random real number between 0.1 and 0.9. More generally:
math.random() * (hi - lo) + lo
which you can wrap in your own function if you like.
But I'll note that that's a fairly peculiar range. If you really want a random number selected from 0.1, 0.2, 0.3, 0.4, ..., 0.9, then you should generate an integer in the range 1 to 9 and then divide it by 10.0:
math.random(1, 9) / 10.0
Keep in mind that most real numbers cannot be represented exactly in floating-point.
You can use math.random() (no args) to generate a number between 0 and 1, and use that to blend between your two target numbers.
-- generates a random real number between a (inclusive) and b (exclusive)
function rand_real(a, b)
return a + (b - a) * math.random()
end
(math.random(10,90)) / 100
This generates a number from 10 to 90 and the division gives you a number from 0.1 to 0.9.

Resources