How to generate random float in lua? - random

I need to generate random float in Lua. It needs to be > 1, so math.random() is not a solution.
How can I do this?

This should generate random floats between 1 (inclusive) and 100 (exclusive)
math.random() + math.random(1, 99)

You can also use something like this to get a number between lower and greater
function randomFloat(lower, greater)
return lower + math.random() * (greater - lower);
end

Just posting for fun, but you can use math.random() with no arguments to do this :P
print(math.floor((math.random()*100)+0.5))

Related

Add percentage to number in Ruby

How can I add a percentage to a number in Ruby?
In this example I want to add 20% to 32.92:
irb(main):001:0> 32.92 * (1 + (20 / 100))
=> 32.92
Google answers with the correct answer; 39.50.
Lets say your base_value is: 39.92.
Your markup is 20.
Integer division will lead to the following:
20 / 100
# => 0
So irb is the right direction. This gives better results:
20.to_f / 100
# => 0.2
So the final calculation will look like this:
final_value = (base_value + (markup.to_f / 100) * base_value).round
This gives you the expected value.
As you don’t mind the result to be floored instead of rounded it’s possible to get correct result using integer division:
final_value = base_value + base_value * markup / 100
20 / 100 returns 0, because it's integer division if you pass integers as arguments. Instead, you can pass floats, like this:
32.92 * (1 + (20.0 / 100.0))
or do simply:
32.92 * 1.2

15 digit floating variable calculation in microcontroller

I want to calculate an equation within a controller(Arduino)
y = -0.0000000104529251928664x^3 + 0.0000928316793270531x^2 - 0.282333029643959x + 297.661280719026
Now the decimal values of the coefficients are important because "x" varies in thousands so cube term cannot be ignored. I have tried manipulating the equation in excel to reduce the coefficients but R^2 is lost in the process and I would like to avoid that.
Max variable size available in Arduino is 4byte. And on google search, I was not able to find an appropriate solution.
Thank you for your time.
Since
-0.0000000104529251928664 ^ (1/3) = - 0.0021864822
0.0000928316793270531 ^ (1/2) = 0.00963491978
The formula
y = -0.0000000104529251928664x^3 + 0.0000928316793270531x^2 - 0.282333029643959x + 297.661280719026
Can be rewritten:
y = -(0.0021864822 * x)^3 + (0.00963491978 * x)^2 - 0.282333029643959 * x + 297.661280719026
Rounding all coefficients to 10 decimal places, we get:
y = -(0.0021864822 * x)^3 + (0.00963491978 * x)^2 - 0.2823330296 * x + 297.6612807
But I don't know Arduino, I'm not sure what the correct number of decimal places is, nor do I know what the compiler will accept or refuse.

Julia : random normal distribution

l want to generate random normal distribution between 0.1 and 0.3 using
randn() how can l use it ?
l tried this one but it's not working
randn(0.1:0.3,(3,1)) # (3,1) three lines and one column
Try doing
randn(3) * (0.3 - 0.1) + 0.1
random number in a range [a,b] is
rand() * (b - a) + a

Generating a random float with a controlled precision level

This generates a random float with a certain precision level in Ruby:
def generate(min,max,precision)
number = rand * (max - min) + min
factor = 10.0 ** precision
return (number * factor).to_i / factor
end
Someone recently suggested me that it may be simpler to do this:
var = rand(100) * 1.0
var /= 10
Which generates a random float from 0.0 to 10.0.
This sounds good, but I'm not sure about how to control the precision level with that method. How may I make the equivalent to my first method, but using this suggestion?
If you have a range x1 to x2, and you want a minimum increment ("precision") of delta, then you need
n = (x2 - x1)/delta +1
Different integers, which you scale with
rand(n) * delta + x1
To give random numbers between x1 and x2 inclusive, with an increment of delta
How do you define "precision"? The relationship between delta and precision is given ( according to you formula) by
Delta = 10**(-precision)
Or
delta = 1.0 / 10**precision

A better Ruby implementation of round decimal to nearest 0.5

This seems horrible inefficient. Can someone give me a better Ruby way.
def round_value
x = (self.value*10).round/10.0 # rounds to two decimal places
r = x.modulo(x.floor) # finds remainder
f = x.floor
self.value = case
when r.between?(0, 0.25)
f
when r.between?(0.26, 0.75)
f+0.5
when r.between?(0.76, 0.99)
f+1.0
end
end
class Float
def round_point5
(self * 2).round / 2.0
end
end
A classic problem: this means you're doing integer rounding with a different radix. You can replace '2' with any other number.
Multiply the number by two.
round to whole number.
Divide by two.
(x * 2.0).round / 2.0
In a generalized form, you multiply by the number of notches you want per whole number (say round to .2 is five notches per whole value). Then round; then divide by the same value.
(x * notches).round / notches
You can accomplish this with a modulo operator too.
(x + (0.05 - (x % 0.05))).round(2)
If x = 1234.56, this will return 1234.6
I stumbled upon this answer because I am writing a Ruby-based calculator and it used Ruby's Money library to do all the financial calculations. Ruby Money objects do not have the same rounding functions that an Integer or Float does, but they can return the remainder (e.g. modulo, %).
Hence, using Ruby Money you can round a Money object to the nearest $25 with the following:
x + (Money.new(2500) - (x % Money.new(2500)))
Here, if x = $1234.45 (<#Money fractional:123445 currency:USD>), then it will return $1250.00 (#
NOTE: There's no need to round with Ruby Money objects since that library takes care of it for you!

Resources