How to select a number randomly in a range but with considerable difference - Gideros Forum

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Top Posters

• ar2rsawseen 6991
• SinisterSoft 3812
• hgy29 2462
• keszegh 2055
• antix 2033
• OZApps 1983
• totebo 1764
• oleg 1574
• hgvyas123 1412
• techdojo 1321
• phongtt 1029
• Mells 1024
• MikeHart 1020
• john26 995
• GregBUG 962
• pie 960
• talis 908
• Scouser 898
• Apollo14 868
• MoKaLux 841

# How to select a number randomly in a range but with considerable difference

edited June 2014
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?

• • Or did you mean something like this?
 `math.random(x, x+40)-20`
Or like this?
 ```if math.random() >= 0.5 then x = x + 20 else x = x - 20 end```
• @ar2rsawseen, it can also be used as
 `math.random(20) + x`
which would ensure that the minimum lower value is never zero, but x.
twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
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
+1 -1 (+2 / -0 ) Share on Facebook
• Accepted Answer
by this
 ```local myTemnum = 50   function 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 +30   end   stage:addEventListener(Event.ENTER_FRAME, displayRand) Timer.delayedCall(500,function() stage:removeEventListener(Event.ENTER_FRAME, displayRand) end)```
• edited June 2014
if x is a continuous variable so
 `x = 20*math.random() + x`
 `P(x=0) = 0`
:x

elseif x is a discrete variable so
@ar2rsawseen, it can also be used as
 `math.random(20) + x`
which would ensure that the minimum lower value is never zero, but x.
end

• Thank you all  It's such a great place :-h
• if x is a continuous variable so
 `x = 20*math.random() + x`
P(x=0) = 0 :x

elseif x is a discrete variable so
@ar2rsawseen, it can also be used asmath.random(20) + x 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.
twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
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
• edited June 2014

@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 == 0 then x = math.random(-20,-1)   elseif left_or_right == 1 then 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```
Second case: x is a continuous variable (Uniform Distribution Continuous)
 `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 Cheers
• That insight was helpful Thank you