Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to select a number randomly in a range but with considerable difference — Gideros Forum

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

simransimran Member
edited June 2014 in General questions
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?

Comments

  • ar2rsawseenar2rsawseen Maintainer
    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
  • hgvyas123hgvyas123 Guru
    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)
  • HubertRonaldHubertRonald Member
    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

  • simransimran Member
    Thank you all :) :D 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 as
    math.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
  • HubertRonaldHubertRonald Member
    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
  • simransimran Member
    That insight was helpful :) Thank you
Sign In or Register to comment.