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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:58:41+00:00 2026-06-09T10:58:41+00:00

I ran into the challenge where I need a function that returns a random

  • 0

I ran into the challenge where I need a function that returns a random number within a given range from 0 - X. Not only that, but I require the number returned to be unique; not duplicating numbers that have already been returned on previous calls to the function.

Optionally, when this is done (e.g. the range has been ‘exhausted’), just return a random number within the range.

How would one go about doing this?

  • 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-09T10:58:45+00:00Added an answer on June 9, 2026 at 10:58 am

    I wrote this function. It keeps its own array with a history of generated numbers, preventing initial duplicates, continuing to output a random number if all numbers in the range have been outputted once:

    // Generates a unique number from a range
    // keeps track of generated numbers in a history array
    // if all numbers in the range have been returned once, keep outputting random numbers within the range
    var UniqueRandom = { NumHistory: new Array(), generate: function(maxNum) {
            var current = Math.round(Math.random()*(maxNum-1));
            if (maxNum > 1 && this.NumHistory.length > 0) {
                if (this.NumHistory.length != maxNum) {
                    while($.inArray(current, this.NumHistory) != -1) { current = Math.round(Math.random()*(maxNum-1)); }
                    this.NumHistory.push(current);
                    return current;
                } else {
                    //unique numbers done, continue outputting random numbers, or we could reset the history array (NumHistory = [];)
                    return current;
                }
            } else {
                //first time only
                this.NumHistory.push(current);
                return current;
            }
        }
    };
    

    Here’s a working Fiddle

    I hope this is of use to someone!

    Edit: as pointed out by Pointy below, it might get slow with a large range (here is a
    fiddle, going over a range from 0-1000, which seems to run fine). However; I didn’t require a very large range, so perhaps this function is indeed not suited if you look to generate and keep track of an enormous range.

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

Sidebar

Related Questions

I ran into a strange issue with tinyMCE that i was not able to
Ran into an issue today that I have not been able to resolve. I
Ran into another challenge. I looked through some of the questions that I found
I ran into the problem that my primary key sequence is not in sync
I ran into a small problem and I'm sure that somebody from the stackoverflow
I ran into a problem that suggests I may be implementing a design pattern
I ran into the case that when I had 4G off and connected to
I ran into some trouble while creating a C-Extension for ruby that got me
I ran into an error that states XPath failed due to: A sequence of
Ran into a problem. I have a spawned thread that must call its finally

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.