Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8170617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:10:58+00:00 2026-06-06T21:10:58+00:00

I have a Node.js process (setInterval) that runs every 100ms. I have certain actions

  • 0

I have a Node.js process (setInterval) that runs every 100ms. I have certain actions that I want to take every x period of time. So for example, 2% of the time do X, 10% of the time do Y, etc.

Right now, I’m basically doing it like this:

var rand = Math.floor(Math.random() * (1000 + 1));

if(rand > 900) {  // Do something }

if(rand > 950) {  // Do something }

The problem is it’s very inconsistent. You would want if(rand > 900) to be at least close to 10% of the time, but sometimes it may be 10x in a row or not at all.

Would anyone have suggestions to a better solution that would be more accurate if we assume the 100ms interval is fixed.

Thank you!

Edit: Based on Dr. Dredel’s comments:

var count = 0;
setInterval(function(){

    if(count++ % 4 == 0) {
       console.log('25% of the time');
    }

}, 100);​
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:10:59+00:00Added an answer on June 6, 2026 at 9:10 pm

    If your interval is fixed, I would round your stamp to the nearest hundred and then use those segments that relate to your needs… 100 and 200 but not 300- 1000 to represent 2%.

    If you CAN use a counter, then that’s the more obvious way to do it.

    if(myCounter++ % 4 == 0)
        //this happens 25 percent of the time 
    

    As Emil points out, probability is not the correct approach here, and I don’t get the sense that you’re married to it… It sounds like you’re using it because you didn’t see a better way to provoke something to happen x% of the time. If we’re misunderstanding you, you need to explain in better detail why you’re using odds here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a node.js server that pretty much runs a child process and sends
Let me explain best with an example. Say you have node class that can
We have a HPC node that runs some of our tasks in it. I
I have a node.js script on my server that I want to run continuously
I have a node.js (v0.6.12) application that starts by evaluating a Javascript file, startup.js.
I have a node with many CCK fields. I want to hide a field
I have a node application that is not a web application - it completes
I'm in the process of theming a node form. I want authenticated users to
I have a node.js script that does some logging to a file using WriteStream.
We are developing a Data Synapse calc node process in C# that requires functionality

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.