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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:00:27+00:00 2026-05-24T17:00:27+00:00

Ok so i need to create four randomly generated numbers between 1-10 and they

  • 0

Ok so i need to create four randomly generated numbers between 1-10 and they cannot be the same. so my thought is to add each number to an array but how can I check to see if the number is in the array, and if it is, re-generate the number and if it isnt add the new number to the array?

so basically it will go,

1.create new number and add to array
2.create second new number, check to see if it exist already, if it doesn’t exist, add to array. If it does exist, re-create new number, check again etc…
3.same as above and so on.

  • 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-24T17:00:27+00:00Added an answer on May 24, 2026 at 5:00 pm

    You want what is called a ‘random grab bag’. Consider you have a ‘bag’ of numbers, each number is only represented once in this bag. You take the numbers out, at random, for as many as you need.

    The problem with some of the other solutions presented here is that they randomly generate the number, and check to see if it was already used. This will take longer and longer to complete (theoretically up to an infinite amount of time) because you are waiting for the random() function to return a value you don’t already have (and it doesn’t have to do that, it could give you 1-9 forever, but never return 10).

    There are a lot of ways to implement a grab-bag type solution, each with varying degrees of cost (though, if done correctly, won’t ever be infinite).

    The most basic solution to your problem would be the following:

    var grabBag = [1,2,3,4,5,6,7,8,9,10];
    
    // randomize order of elements with a sort function that randomly returns -1/0/1
    grabBag.sort(function(xx,yy){ return Math.floor(Math.random() * 3) - 1; })
    
    function getNextRandom(){
        return grabBag.shift();
    };
    
    var originalLength = grabBag.length;
    for(var i = 0; i < originalLength; i++){
        console.log(getNextRandom());
    }
    

    This is of course destructive to the original grabBag array. And I’m not sure how ‘truly random’ that sort is, but for many applications it could be ‘good enough’.

    An slightly different approach would be to store all the unused elements in an array, randomly select an index, and then remove the element at that index. The cost here is how frequently you are creating/destroying arrays each time you remove an element.

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

Sidebar

Related Questions

I need to create a 2D int array of size 800x800. But doing so
I need to create a home screen (menu) with four to six items arranged
I need to create a WAV file in PHP that consists of four repetitions
I need to create a MySQL query that generates 4 rows for each row
I need to create a nested list, four levels deep. On the fourth level,
I need to create a pdf with four equal length columns across the page
I need to create four custom folders that are on the root of the
I need to create a strategy pattern where a user selects four strategies from
I need create custom dialog and put JPanel into it. Is it possible?
i need create an email list sending to many emails. what is best solution

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.