This is my code
while true do
script.Parent.Position = Vector3.new((math.random(-41.994,15.471)),0.5,(math.random(129.514,69.442)))
script.Parent.Color = Color3.new(math.random(0,255), math.random(0,255), math.random(0,255))
wait(1)
end
The programming language I am using is Lua
When I try to use this code I am presented with this error:
"15:50:47.926 - Workspace.rock outer walls.Model.Rocks.Part0.Script:2: bad argument #2 to 'random' (interval is empty)"
The purpose of the code is to randomly teleport the part the script is in around but not to far away and at the same y axis.
Can somebody please give me some form of explanation
Ps. A while ago I made a rude post on this website because I was confused at how to do a lot of things and now I understand some stuff better so I would like to apologize for my idiocy ~Zeeen
In Lua, math.random can be called 3 ways:
with no arguments
with 1 integer argument
with 2 integer arguments
It does not accept values like -41.994 or 15.471 this is why you're getting the error.
If your change your values to -41 or 15 you shouldn't see an error any more.
Lua 5.3 reference manual: http://www.lua.org/manual/5.3/manual.html#pdf-math.random
math.random ([m [, n]])
When called without arguments, returns a pseudo-random float with uniform distribution in the range [0,1). When called with two integers m and n, math.random returns a pseudo-random integer with uniform distribution in the range [m, n]. (The value n-m cannot be negative and must fit in a Lua integer.) The call math.random(n) is equivalent to math.random(1,n).
This function is an interface to the underling pseudo-random generator function provided by C.
As Nifim's answer correctly points out, there are three ways to call math.random in Lua.
With no arguments, it returns a real number in the range 0.0 to 1.0.
With one or two integer arguments, it returns an integer.
None of these directly give you want you want, which I presume is a random real number in a specified range.
To do that, you'll need to call math.random with no arguments and then adjust the result.
For example, if you wanted a random number between 5.0 and 10.0, you could use
math.random() * 5.0 + 5.0
Consider writing your own wrapper function that takes two floating-point arguments and calls math.random.
function random_real(x, y)
return x + math.random() * (y-x)
end
Related
We had given task as
Read an angle in degrees, and print the tan, cosec, sec, and cot values of that angle in the same order. To use via the rad() function, convert the angle from degrees to radians for the standard library functions.
For which we written code as
x = io.read()
radianVal = math.rad (x)
io.write(string.format("%.1f ", math.tan(radianVal)),"\n")
io.write(string.format("%.1f ", math.cosh(radianVal)),"\n")
io.write(string.format("%.1f ", math.sinh(radianVal)),"\n")
io.write(string.format("%.1f ", math.cos(radianVal)),"\n")
But output is not as expect, not sure where we went wrong
Run as Custom Input
30
Your Output (stdout)
0.6
1.1
0.5
0.9
Expected Output
0.57735026918963
2.0
1.1547005383793
1.7320508075689
Correct code which is working perfectly is
x = io.read()
function cot(radAngle)
return 1 / math.tan(radAngle)
end
function cosec(radAngle)
return 1 / math.sin(radAngle)
end
function sec(radAngle)
return 1 / math.cos(radAngle)
end
local radAngle = math.rad(x)
print(math.tan(radAngle))
print(cosec(math.rad(x)))
print(sec(math.rad(x)))
print(cot(math.rad(x)))
How can you expect to get the correct result if you calculate cosh(x) instead of csc(x)? You're using the hyperbolic cosine to calcuate the cosecant. I think it is obvious why your results are not accepted.
You use sinh(x) to calculate sec(x) and cos(x) to calculate cot(x).
You're confusing mathematical functions. That's not a Lua programming problem.
I suggest you spend a few hours on researching trigonometric functions.
https://en.wikipedia.org/wiki/Trigonometric_functions
Such values are calcuated using the correct formula or function. Not by using any function of the math library that has a similar name.
cosh is deprecated since Lua 5.3 btw.
You can use math.tan to calculate the tangens of your angle.
For secant, cosecant and cotangens you'll have to implement your own functions.
Also note that the number of decimals might cause issue in result validation. Make sure it is ok to round to 1 decimal.
Example:
There is no function to calculate the cotangent of an angle. So we define one.
function cot(radAngle)
return 1 / math.tan(radAngle)
end
local radAngle = math.rad(30)
print("tan(30°):", math.tan(radAngle))
print("cot(30°):", cot(math.rad(30)))
->
tan(30°): 0.57735026918963
cot(30°): 1.7320508075689
I am working on programming a Markov chain in Lua, and one element of this requires me to uniformly generate random numbers. Here is a simplified example to illustrate my question:
example = function(x)
local r = math.random(1,10)
print(r)
return x[r]
end
exampleArray = {"a","b","c","d","e","f","g","h","i","j"}
print(example(exampleArray))
My issue is that when I re-run this program multiple times (mash F5) the exact same random number is generated resulting in the example function selecting the exact same array element. However, if I include many calls to the example function within the single program by repeating the print line at the end many times I get suitable random results.
This is not my intention as a proper Markov pseudo-random text generator should be able to run the same program with the same inputs multiple times and output different pseudo-random text every time. I have tried resetting the seed using math.randomseed(os.time()) and this makes it so the random number distribution is no longer uniform. My goal is to be able to re-run the above program and receive a randomly selected number every time.
You need to run math.randomseed() once before using math.random(), like this:
math.randomseed(os.time())
From your comment that you saw the first number is still the same. This is caused by the implementation of random generator in some platforms.
The solution is to pop some random numbers before using them for real:
math.randomseed(os.time())
math.random(); math.random(); math.random()
Note that the standard C library random() is usually not so uniformly random, a better solution is to use a better random generator if your platform provides one.
Reference: Lua Math Library
Standard C random numbers generator used in Lua isn't guananteed to be good for simulation. The words "Markov chain" suggest that you may need a better one. Here's a generator widely used for Monte-Carlo calculations:
local A1, A2 = 727595, 798405 -- 5^17=D20*A1+A2
local D20, D40 = 1048576, 1099511627776 -- 2^20, 2^40
local X1, X2 = 0, 1
function rand()
local U = X2*A2
local V = (X1*A2 + X2*A1) % D20
V = (V*D20 + U) % D40
X1 = math.floor(V/D20)
X2 = V - X1*D20
return V/D40
end
It generates a number between 0 and 1, so r = math.floor(rand()*10) + 1 would go into your example.
(That's multiplicative random number generator with period 2^38, multiplier 5^17 and modulo 2^40, original Pascal code by http://osmf.sscc.ru/~smp/)
math.randomseed(os.clock()*100000000000)
for i=1,3 do
math.random(10000, 65000)
end
Always results in new random numbers. Changing the seed value will ensure randomness. Don't follow os.time() because it is the epoch time and changes after one second but os.clock() won't have the same value at any close instance.
There's the Luaossl library solution: (https://github.com/wahern/luaossl)
local rand = require "openssl.rand"
local randominteger
if rand.ready() then -- rand has been properly seeded
-- Returns a cryptographically strong uniform random integer in the interval [0, n−1].
randominteger = rand.uniform(99) + 1 -- randomizes an integer from range 1 to 100
end
http://25thandclement.com/~william/projects/luaossl.pdf
I am using lua 5.3.2 and the following piece of code gives me an error:
string.format("%d", 1.16 * 100)
whereas the following line works fine
string.format("%d", 1.25 * 100)
This is probably related to this question but the failure depends on the floating point value. Given that, in my case, a local variable (v) holds the float value, and is generated by an expresson that produces a value between 0 and 2 rounded to 2 decimal places.
How can I modify the code to ensure this doesn't fail for any possible value of v ?
You can use math.floor to convert to integer and add +0.5 if you need to round it: math.floor(1.16 * 100 + 0.5). Alternatively, "%.0f" should have the desired effect as well.
I expected that the result would be 87.29. I also tried SimpleRoundTo, but produces the same result.
In the help there is also a "strange" example:
ms-help://embarcadero.rs2010/vcl/Math.RoundTo.html
RoundTo(1.235, -2) => 1.24
RoundTo(1.245, -2) => 1.24 //???
Does anybody know which function I need to get the result of 87.29? I mean: If the last digit >= 5 round up, if < 5 round down. As taught in the school :)
I use Delphi2010, and SetRoundMode(rmNearest). I also tried with rmTruncate.
The value 87.285 is stored in a double variable.
Also strange:
SimpleRoundTo(87.285, -2) => 87.29
but
x := 87.285; //double
SimpleRoundTo(x, -2) => 87.28
The exact value 87.285 is not representable as a floating-point value in Delphi. A page on my Web site shows what that value really is, as Extended, Double, and Single:
87.285 = + 87.28500 00000 00000 00333 06690 73875 46962 12708 95004 27246 09375
87.285 = + 87.28499 99999 99996 58939 48683 51519 10781 86035 15625
87.285 = + 87.28500 36621 09375
By default, floating-point literals in Delphi have type Extended, and as you can see, the Extended version of your number is slightly higher than 87.285, so it is correct that rounding to nearest would round up. But as a Double, the real number is slightly lower. That's why you get the number you expected if you explicitly store the number in a Double variable before calling RoundTo. There are overloads of that function for each of Delphi's floating-point types.
87.285 is not exactly representable and the nearest double is slightly smaller.
The classic reference on floating point is What Every Computer Scientist Should Know About Floating-
Point Arithmetic.
For currency based calculations, if indeed this is, you should use a base 10 number type rather than base 2 floating point. In Delphi that means Currency.
I created a calculator class that does basic +,-, %, * and sin, cos, tan, sqrt and other math functions.
I have all the variables of type double, everything is working fine for big numbers, so I can calculate numbers like 1.35E122, but the problem is with extremely small numbers. For example if I do calculation 1/98556321 I get 0 where I would like to get something 1.01464E-8.
Should I rewrite my code so that I only manipulate NSDecimalNumber's and if so, what do I do with sin and cos math functions that accept only double and long double values.
1/98556321
This division gives you 0 because integer division is performed here - the result is an integer part of division. The following line should give you floating point result:
1/(double)98556321
integer/integer is always an integer
So either you convert the upper or the lower number to decimal
(double)1/98556321
or
1/(double)98556321
Which explicitely convert the number to double.
Happy coding....