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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:16:14+00:00 2026-05-31T05:16:14+00:00

I have a simple code which generates random numbers SecureRandom random = new SecureRandom();

  • 0

I have a simple code which generates random numbers

SecureRandom random = new SecureRandom();
...
public int getRandomNumber(int maxValue) {
    return random.nextInt(maxValue);
}

The method above is called about 10 times (not in a loop). I want to ensure that all the numbers are unique (assuming that maxValue > 1000).

Can I be sure that I will get unique numbers every time I call it? If not, how can I fix it?

EDIT: I may have said it vaguely. I wanted to avoid manual checks if I really got unique numbers so I was wondering if there is a better solution.

  • 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-31T05:16:15+00:00Added an answer on May 31, 2026 at 5:16 am

    There are different ways of achieving this and which is more appropriate will depend on how many numbers you need to pick from how many.

    • If you are selecting a small number of random numbers from a large range of potential numbers, then you’re probably best just storing previously chosen numbers in a set and “manually” checking for duplicates. Most of the time, you won’t actually get a duplicate and the test will have practically zero cost in practical terms. It might sound inelegant, but it’s not actually as bad as it sounds.
    • Some underlying random number generation algorithms don’t produce duplicates at their “raw” level. So for example, an algorithm called a XORShift generator can effectively produce all of the numbers within a certain range, shuffled without duplicates. So you basically choose a random starting point in the sequence then just generate the next n numbers and you know there won’t be duplicates. But you can’t arbitrarily choose “max” in this case: it has to be the natural maximum of the generator in question.
    • If the range of possible numbers is small-ish but the number of numbers you need to pick is within a couple of orders of magnitude of that range, then you could treat this as a random selection problem. For example, to choose 100,000 numbers within the range 10,000,000 without duplicates, I can do this:

      Let m be the number of random numbers I’ve chosen so far

      For i = 1 to 10,000,000

      Generate a random (floating point) number, r, in the range 0-1

      If (r < (100,000-m)/(10,000,000-i)), then add i to the list and increment m

      Shuffle the list, then pick numbers sequentially from the list as required

    But obviously, there’s only much point in choosing the latter option if you need to pick some reasonably large proportion of the overall range of numbers. For choosing 10 numbers in the range 1 to a billion, you would be generating a billion random numbers when by just checking for duplicates as you go, you’d be very unlikely to actually get a duplicate and would only have ended up generating 10 random numbers.

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

Sidebar

Related Questions

I have some code I've written in PHP for consuming our simple webservice, which
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
I have a simple code byte[] buffer = Encoding.UTF8.GetBytes(abracadabra); MemoryStream ms = new MemoryStream();
I have a C makefile which generates header file from java code and compiles
A simple problem. I have an ASHX handler which generates a report. Unfortunately, this
I have a simple questionnaire system which shows a random number of questions to
Let's say I have a server side script which generates JSON from a simple
I have a simple application which takes a text and password, generates a text
I have the following sample code which doesn't seem to want to run. import
Does anyone have sample code, or a tutorial which demonstrates how to use Grappa

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.