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

  • SEARCH
  • Home
  • 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 8034183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:55:49+00:00 2026-06-05T01:55:49+00:00

I’m trying to create a javascript function that accepts 2 parameters min and max

  • 0

I’m trying to create a javascript function that accepts 2 parameters min and max and generates a random number between the two integers. That part is easy.

Where things get rough is that I need to create a conditional that says

function generateNum (min, max) {
  var randNumber = Math.ceil(Math.random()*(max - min)+min);

  if (randNumber === max) {
    // store the result (probably in an array) and generate a new 
    // number with the same behavior as randNumber (e.g. it is also
    // stores it's total in the array and recursively "re-generates 
    // a new number until max is not hit)
  }
}

The idea is to recursive-ise this so that a running total of the number of max hits is stored, combined, and then returned.

For example: The script receives min / max parameters of 5/10 generateNum(5,10){}. If the value generated by randNumber were 5, 6, 7, 8, or 9 then there would be no recursion and the function would return that value. If the value generated by randNumber is 10, then the value 10 is stored in an array and the function now “re-tries” recursively (meaning that as many times as 10 is generated, then that value is stored as an additional object in the array and the function re-tries). When the process stops (which could be infinite but has a parabolically decreasing probability of repeating with each recursion). The final number (5, 6, 7, 8, 9) would be added to the total of generated max values and the result would be returned.

Quite an unusual mathematic scenario, let me know how I can clarify if that doesn’t make sense.

  • 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-05T01:55:51+00:00Added an answer on June 5, 2026 at 1:55 am

    That part is easy.

    Not as easy as you think… The algorithm that you have is broken; it will almost never give you the minimum value. Use the Math.floor method instead, and add one to the range:

    var randNumber = Math.floor(Math.random() * (max - min + 1)) + min;
    

    To do this recursively is simple, just call the method from itself:

    function generateNum (min, max) {
      var randNumber = Math.floor(Math.random()*(max - min + 1)) + min;
      if (randNumber == max) {
        randNumber += generateNum(min, max);
      }
      return randNumber;
    }
    

    You can also solve this without recursion:

    function generateNum (min, max) {
      var randNumber = 0;
      do {
        var num = Math.floor(Math.random()*(max - min + 1)) + min;
        randNumber += num;
      } while (num == max);
      return randNumber;
    }
    

    There is no need to use an array in either case, as you don’t need the seprate values in the end, you only need the sum of the values.

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small

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.