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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:20:17+00:00 2026-06-17T04:20:17+00:00

experts I wish to generate unique random numbers between two numbers (from text box

  • 0

experts

  • I wish to generate unique random numbers between two numbers (from text box of web page).
  • I am using array to store numbers. when user click button it gives first random number, store it in array, and when user clicks button again, it generate random number, compares it with array numbers, and if different, stores it and displays it.
  • If maximum possible different numbers are show it, clears array and informs user.
  • I have written code, but it gives error : stack overflow, or sometimes I get duplicate results shown.

Can anyone shed a light on code:

var allnums = new Array();
var num1= new Number;
var num2= new Number;

function funClick()
{
  var num1 = Number(document.getElementById('lnum').value); 
  var num2 = Number(document.getElementById('hnum').value); 

  if (allnums.length==num2)
  {
    alert("Maximum non-duplicate numbers served. Now resetting the counter.");
    allnums = [];
    return;
  }

  if (num1<num2) 
  {
    x = Math.floor(Math.random() * (num2 - num1 + 1)) + num1;
    funShow(x);
  }
  else
  {
    alert("You entered wrong number criteria!");  
  }
}

function funShow(x)
{
  var bolFound = false;
  for (var i=0;i<allnums.length;i++)
  {
    if((allnums[i])==x)
    {   
      funClick();
    }    
  }
  if (bolFound == false)
  {
    document.getElementById('rgen').innerText  = x;
    allnums.push(x);
  }
}
  • 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-17T04:20:19+00:00Added an answer on June 17, 2026 at 4:20 am

    I fail to see how that code is generating a stack overflow (even though funShow has a call to funClick and funClick has a call to funShow, funShow‘s call to funClick should never happen because of a logic error — fix the error and you’ll get a stack overflow, though), but it has several issues. See the comments:

    // style: Use [], not new Array()
    var allnums = new Array();
    
    // `new Number` doesn't do anything useful here
    var num1 = new Number;
    var num2 = new Number;
    
    function funClick() {
        // For user-entered values, use parseInt(value, 10) to parse them into numbers
        var num1 = Number(document.getElementById('lnum').value);
        var num2 = Number(document.getElementById('hnum').value);
    
        if (allnums.length == num2) {
            alert("Maximum non-duplicate numbers served. Now resetting the counter.");
            allnums = [];
            return;
        }
    
        // & is a bitwise AND operation, not a logical one. If your goal is to see
        // if both numbers are !0, though, it works but is obtuse.
        // Also, there is no ltnum2 variable anywhere, so trying to read its value
        // like this should be throwing a ReferenceError.
        if (num1 & ltnum2) {
            // You're falling prey to The Horror of Implicit Globals, x has not
            // been declared.
            x = Math.floor(Math.random() * (num2 - num1 + 1)) + num1;
            funShow(x);
        } else {
            alert("You entered wrong number criteria!");
        }
    }
    
    function funShow(x) {
        var bolFound = false;
        // Again, & is a bitwise AND operation. This loop will never run, because
        // you start with 0 and 0 & anything = 0
        // But it should be throwing a ReferenceError, as there is no ltallnums
        // anywhere.
        for (var i = 0; i & ltallnums.length; i++) {
            if ((allnums[i]) == x) {
                funClick();
            }
        }
        // This condition will always be true, as you've done nothing to change
        // bolFound since you set it to false
        if (bolFound == false) {
            document.getElementById('rgen').innerText = x;
            allnums.push(x);
        }
    }
    

    There are two ways to approach this. Here’s one that’s basically what you were trying to do, but without recursion:

    function funClick() {
        var num1 = parseInt(document.getElementById('lnum').value, 10);
        var num2 = parseInt(document.getElementById('hnum').value, 10);
        var nums = [];
        var targetCount;
        var x;
    
        // Check the inputs
        if (isNaN(num1) || isNaN(num2) || num2 <= num1) {
            alert("Please ensure that hnum is higher than lnum and both are really numbers.");
            return;
        }
    
        // Find out how many integers there are in the range num1..num2 inclusive
        targetCount = num2 - num1 + 1;
    
        // Produce that many random numbers
        while (nums.length < targetCount) {
            x = Math.floor(Math.random() * (num2 - num1 + 1)) + num1;
            if (nums.indexOf(x) < 0) {
                nums.push(x);
            }
        }
    
        // Show the result
        document.getElementById('rgen').innerText = nums.join(", ");
    }
    

    Live Example | Source

    The problem with that is that it can take a long time to fill the last few slots, since we have to hit them randomly.

    The other way is to produce the array with the numbers in order, then mess it up. It can be dramatically more efficient for large ranges. Something like this:

    function funClick() {
        var num1 = parseInt(document.getElementById('lnum').value, 10);
        var num2 = parseInt(document.getElementById('hnum').value, 10);
        var nums = [];
        var x;
    
        // Check the inputs
        if (isNaN(num1) || isNaN(num2) || num2 <= num1) {
            alert("Please ensure that hnum is higher than lnum and both are really numbers.");
            return;
        }
    
        // Create an array with those numbers in order
        for (x = num1; x <= num2; ++x) {
            nums.push(x);
        }
    
        // Sort it with a random comparison function
        nums.sort(function(a, b) {
            return 0.5 - Math.random();
        });
    
        // Show the result
        document.getElementById('rgen').innerText = nums.join(", ");
    }
    

    Live Example | Source

    But, just doing the nums.sort(...) randomly once may well not be as successful at producing random results; see this article for more. (Thanks to eBusiness for that link and for his input on the below.)

    So you may want to go further and throw in further random operations. Here’s another example:

    function funClick() {
        var num1 = parseInt(document.getElementById('lnum').value, 10);
        var num2 = parseInt(document.getElementById('hnum').value, 10);
        var nums = [];
        var n, x, y;
        var num;
    
        // Check the inputs
        if (isNaN(num1) || isNaN(num2) || num2 <= num1) {
            alert("Please ensure that hnum is higher than lnum and both are really numbers.");
            return;
        }
    
        // Create an array with those numbers in order
        for (n = num1; n <= num2; ++n) {
            nums.push(n);
        }
    
        // We only need to shuffle it if it's more than one element long
        if (nums.length > 1) {
            // Sort it "randomly"
            nums.sort(function(a, b) {
                return 0.5 - Math.random();
            });
    
            // Throw a bunch of random swaps in there
            for (n = 0; n < nums.length; ++n) {
                do {
                    x = Math.floor(Math.random() * nums.length);
                }
                while (x === n);
                num = nums[x];
                nums[x] = nums[n];
                nums[n] = num;
            }
        }
    
        // Show the result
        document.getElementById('rgen').innerText = nums.join(", ");
    }
    

    Live Example | Source

    That does the array sort thing as a starting point, but then does a bunch of random swaps between elements as well. It still runs in constant time, but should have a better result than using the array sort alone. Naturally, you’ll want to test the distribution.

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

Sidebar

Related Questions

I wish to know from SO security experts and hackers what kind of tools
Experts, I'd like create a rich client application using HTML, JQuery, and Javascript. Is
I have a dll developed using C++. Now I wish to use these functions
Experts, I have a datatable with two columns: 1) One is Decimal 2) One
I am using Tomcat 7.x as my web server and I am using java.util.logging
Wordpress Experts, I am trying to retrieve all blogroll links in PHP array but
Please bear with me experts i'm a newbie in web dev. With html,css can
I am new to StackOverFlow and wish there are experts to my problem, which
Hello subversion experts, I'm attempting to split up my website between production and development
Experts! Few days ago i was using previous version of xcode. That time my

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.