Skip to main content
Inspiring
Jul 15, 2022 11:46 PM UTC
Answered

running number increment by a random range

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 is closed to new replies. Start a new post to keep the conversation going.
    kglad
    kgladCorrect answer
    Community Expert
    Jul 16, 2022 3:56 PM UTC

    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.

     

    7 replies

    kglad
    Community Expert
    kgladCommunity ExpertCorrect answer
    Community Expert
    Jul 16, 2022 3:56 PM UTC

    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.

     

    Translate
    Inspiring
    Jul 16, 2022 10:47 PM UTC

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

    Translate
    kglad
    Community Expert
    Community Expert
    Jul 16, 2022 11:34 PM UTC

    you're welcome.

     

    with that info did you decide what you want?

    Translate