• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

running number increment by a random range

Explorer ,
Jul 15, 2022 Jul 15, 2022

Copy link to clipboard

Copied

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.

Views

260

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 16, 2022 Jul 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 i

...

Votes

Translate

Translate
Community Expert ,
Jul 16, 2022 Jul 16, 2022

Copy link to clipboard

Copied

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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 16, 2022 Jul 16, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 16, 2022 Jul 16, 2022

Copy link to clipboard

Copied

you're welcome.

 

with that info did you decide what you want?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 16, 2022 Jul 16, 2022

Copy link to clipboard

Copied

yes, I did. Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 16, 2022 Jul 16, 2022

Copy link to clipboard

Copied

I alternatively choose this way, where I generate the random increment number in dynamic text (nomorAcak) and the other dynamic text (hasil) to show the sum of a specific number (ex: 277, further I will change this with the current number that show in the dynamic text) and the random number by this code:

 

var _this = this;

this.addEventListener("tick", nomoracak.bind(this));
var ini = this
function nomoracak() {
var numberA = [2, 3, 4, 5];
_this.nomorAcak.text = numberA[Math.floor(Math.random() * numberA.length)];
}

this.testbtn.on('click', function () {
_this.hasil.text = 277 + _this.nomorAcak.text
});

It works, But I need this button click trigger to be changed by the time or second, is it possible?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 17, 2022 Jul 17, 2022

Copy link to clipboard

Copied

I've found what I need. I run the time in seconds and read this as a number, then I use this number in a mathematical equation, and got what I want. Thank you for your explanation and attention kglad. It's so helpful to know that what I want in the beginning is impossible so I no longer focus on that and finally found another way.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 17, 2022 Jul 17, 2022

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines