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 6886169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:43:33+00:00 2026-05-27T05:43:33+00:00

I am trying to create a function, that will always total the mx variable,

  • 0

I am trying to create a function, that will always total the mx variable, in the code:

(function () {
    var i = 0;
    var j;
    var mn = 1;
    var mx = 579;

    while (i < mx) {
        j = Math.floor(Math.random() * (mx - mn + 1)) + mn;
        mx = mx - i;
        i += j;

        if (i < (mx - j)) {
            console.log(i);
        } else {
            console.log(mx);
        }
    }
})();

So if mx = 999 then the console may log 900, 90 and 9 or if mx = 50 the console could log 4, 1, 10, 33 and 2.

This happens most of the time, but every now-and-again I get numbers that equal greater than mx and I can’t for the life of me figure out why!

I’d be very grateful for any help, pointers or direction! As I’d like to lean where I went wrong, an explanation would be really awesome too!

Thank you!

  • 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-05-27T05:43:34+00:00Added an answer on May 27, 2026 at 5:43 am

    I’m not sure I’m interpreting your problem correctly, but you seem to want to “break up” a number into random numbers which add up to the initial value (579).

    If so, I think you’re doing it a little bit incorrectly: you;re both substracting i from max and adding j to i. Thus, i becomes larger each time.

    It looks like your algorithm can be simplified.

    Edit: I gave it another shot so that the numbers are (as far as I’m concerned) more uniformly distributed. I also renamed the variables for clarity:

    (function () {
        var total = 0,
            current,
            min = 1,
            max = 579;
    
        while (total < max) { // while the total has not reached max yet
            current = Math.floor(Math.random() * (max - min + 1)) + min;
    
            if(total + current > max) { // if total would become too high
                current = max - total;  // set current such that total becomes max
            }
    
            total += current;
    
            console.log(current);
        }
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a function that will block access to some of
I am new to Prolog and I'm trying to to create function that will
I'm trying to create javascript closure that will tell me if the function has
I am trying to create a function that will split a string into search
I'm trying to create a function that will take a string which could be
I'm trying to create an export Excel/CSV function that will iterate through a custom
For my program I am trying to create a function that will generate an
I am trying to create a function that will take an int and separately
I've been trying to create a function that will write a minidump file for
I'm trying to create a function that will loop through an array of various

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.