Skip to main content
Inspiring
July 15, 2022
Answered

running number increment by a random range

  • July 15, 2022
  • 1 reply
  • 820 views

Hi, is there anyone who can help me, please? 
I'm trying to create a running number increment by a random range such as (+3, +4, +5, etc). Right now, I've tried to use separate code for this, one to run a random, and the other to run a counter number, and make another dynamic text to sum up both of them, but it's too complicated, and it's hard to control the speed. then I try this code.

 

var start = 25

var end = 130

var f = tickerF.bind(this);

var current = start;

startTickerF(25, 130);

function startTickerF(startNum, endNum) {

start = startNum;

end = endNum;

current = start;

createjs.Ticker.addEventListener('tick', f);
}


function tickerF() {

this.suhu.text = current.toFixed(1); 

var numberA= [ 2, 3, 4, 5 ];

current = current + numberA[Math.floor(Math.random() * numberA.length)];
 } 

 

But it didn't work. I'm sorry if this code is seems unlogic, I'm a beginner, and still trying to learn and understand it.  Thanks in advance.

    This topic has been closed for replies.
    Correct answer kglad

    you need to more carefully explain what you're trying to achieve.  from what you stated and the code you showed (which is i know fails to do what you want), you want to:

     

    start and end a textfield display with a fixed numbers (25 and 130).

    you want to increment the start number AND each incremented number by 2,3,4 or 5 (with those increments being chosen randomly).

     

    if that's true, it's impossible.  you can't be certain (in fact it's unlikely) the last number will be what you want if all the increments are chosen at random.  ie, you'll end on a number between 126 and 134 depending on how you choose your end condition(s), or you can't choose the last number(s) randomly.

     

    1 reply

    kglad
    Community Expert
    kgladCommunity ExpertCorrect answer
    Community Expert
    July 16, 2022

    you need to more carefully explain what you're trying to achieve.  from what you stated and the code you showed (which is i know fails to do what you want), you want to:

     

    start and end a textfield display with a fixed numbers (25 and 130).

    you want to increment the start number AND each incremented number by 2,3,4 or 5 (with those increments being chosen randomly).

     

    if that's true, it's impossible.  you can't be certain (in fact it's unlikely) the last number will be what you want if all the increments are chosen at random.  ie, you'll end on a number between 126 and 134 depending on how you choose your end condition(s), or you can't choose the last number(s) randomly.

     

    Inspiring
    July 16, 2022

    yap, that's exactly what I want. Alright, I see. thank you very much for your explanation. 

    kglad
    Community Expert
    Community Expert
    July 16, 2022

    you're welcome.

     

    with that info did you decide what you want?