I want to select a random number within a range, but say number that was gets generated is 'x', next time i want the number to be at least x(+)or(-) 20. How can I achieve this?
local myTemnum =50function displayRand()local rand =math.random(myTemnum-10,myTemnum+10)
myTemnum = rand
print("your random num is with minimum range of 20 with last number is ",myTemnum)
myTemnum = myTemnum +30end
stage:addEventListener(Event.ENTER_FRAME, displayRand)
Timer.delayedCall(500,function()
stage:removeEventListener(Event.ENTER_FRAME, displayRand)end)
which would ensure that the minimum lower value is never zero, but x.
end
@HubertRonald, you are correct that when x = 0, the value can be 0. However the point here was to achieve a random value starting with a minimum value of x.
@HubertRonald, you are correct that when x = 0, the value can be 0. However the point here was to achieve a random value starting with a minimum value of x.
Hi! @OZApps P(x=0) = 0 mean than probability of that x will be zero is zero because x~[min, max] (x is a pseudo random continuous variable) there are infinities values then:
1/math.huge =0
So no matter the initial value of x or after, because it is guaranteed to never x will be zero.
For example, we are make a classic: "Asteroids Game" and we define the rotational speed of our rocks that appear in the game in a range of [-20, 20] and we always want our rocks are rotating:
x: Rotational speed
First case: x is a discrete value:
left_or_right =random.math(0,1)if left_or_right ==0then
x =math.random(-20,-1)elseif left_or_right ==1then
x =math.random(1,20)end-- or any algorithm like a sort ([-20,-19,...,-1,1,...,19,20]) zero isn't in this list-- or another algorithm
x =40*math.random() - 20-- x will not be zero: P(x=0) = 0
I know that program has some art because there is no one right way to do a code (and such flexibility that I love :x), just wanted to bring some of my views with my folks.
But most important is that our friend @simran found his solution
Comments
a = math.random(20, x)
http://docs.giderosmobile.com/reference/lua/math/random#math.random
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
Likes: ar2rsawseen, HubertRonald
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
elseif x is a discrete variable so end
[-] Liasoft
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
P(x=0) = 0 mean than probability of that x will be zero is zero because x~[min, max] (x is a pseudo random continuous variable) there are infinities values then:
For example, we are make a classic: "Asteroids Game" and we define the rotational speed of our rocks that appear in the game in a range of [-20, 20] and we always want our rocks are rotating:
x: Rotational speed
First case: x is a discrete value:
But most important is that our friend @simran found his solution
Cheers
[-] Liasoft