answersLogoWhite

0


Best Answer

Since you (hopefully) can't predict random numbers in Java, we'll need to store the numbers that we've generated so far. A Set seem ideal for this, as it automatically handles keeping itself free of duplicates. The problem is that the order in which elements are removed from a set are not guaranteed to be in the same as the order in which they were added. I would suggest using a List of some sort:

// returns a List of n random integers from 0 to max

static List<Integer> getUniqueRandomInts(final int n, final int max) {

// if n > max, this function would never return, so remove the possibility

if (n > max) {

return null;

}

// new list of values

final List<Integer> list = new ArrayList<Integer>();

// seed our random generator with the current time

final Random rnd = new Random(System.currentTimeMillis());

// keep trying to add numbers until we have the proper number

while (list.size() < n) {

int num = rnd.nextInt(max);

if (!list.contains(num)) {

list.add(num);

}

}

return list;

}

User Avatar

Wiki User

βˆ™ 15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Programming Random numbers without repetition of numbers in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you get a random number from a group of numbers in C?

Random numbers cannot be generated programatically. For pseudo-random numbers use function 'rand'.


Is there such thing random numbers?

yes, because the number generated is from the computer's CPU database and is selected randomly therefore it must be a random number It depends on how the numbers are generated. If they are based on things in the environment, like noise levels, then yes. If they are solely generated by computer functions, then no, they are pseudo-random numbers, which will be random enough for most purposes.


What is Hardware random number generator?

In computing, a hardware random number generator is an apparatus that generates random numbers from a physical process.


How do you generate a random integer within a range in Java?

Take advantage of Java's easy-to-use Random class.// Create a new Random object.// The constructor accepts a single Long argument.// This is the seed for the random generator.// Using the current time is standard for most applications.Random rnd = new Random(System.currentTimeMillis());// A call to nextInt(n) will generate a random value from 0 to n-1// This is typical in programming languages, and in order to get a specific range we need// to tweak it a bit.rnd.nextInt(n);// This will give you a random int from (start) to (start + range - 1)rnd.nextInt(range) + start;


How do you make a random number using Python?

this is the code for making a random number using python: import random &gt;&gt;&gt;random.randint (1, 10) you can do whatever numbers you want.

Related questions

What is a Random Pattern?

A random pattern refers to a design or arrangement that lacks a specific order, sequence, or repetition. It is characterized by a random distribution of elements or motifs without a discernible structure or logic. Random patterns often evoke a sense of spontaneity and unpredictability in visual compositions.


Random numbers without repetition of numbers in jsp or servlet?

The normal way of "generating" random numbers without repetition is to first define an array of all possible values, and then to randomize their order. You can then iterate over the array to get your "random" values. // Returns an array containing the values in the range [start, end] in a random order. static int[] getRandom(final int start, final int end) { final int[] nums = new int[end - start + 1]; // Fill array for (int i = 0; i &lt; nums.length; ++i) { nums[i] = start + i; } // Shuffle array final Random rnd = new Random(System.currentTimeMillis()); for (int i = nums.length - 1; i &gt; 0; --i) { // Generate an index to swap with... final int swapIndex = rnd.nextInt(i + 1); // ...and swap final int temp = nums[i]; nums[i] = nums[swapIndex]; nums[swapIndex] = temp; } return nums; }


What has the author Beth Carson Richardson written?

Beth Carson Richardson has written: 'Random number generation on the I.B.M. 360' -- subject(s): IBM 360 (Computer), Programming, Random Numbers


Explanation for man can only find meaning in life?

Without meaning nothing can be explained.Life is not endemic to any random selection of elements without programming ,forethought and fit for purpose.


How can random numbers be generated in Java?

Random numbers can be generated in Java using the "random" class. One needs a single "random" object to generate a series of random numbers as a unit.


How does the random function work?

The random function generates a random number between 0 (inclusive) and 1 (exclusive) each time it is called. It uses a mathematical algorithm to produce pseudorandom numbers, which appear to be random but are actually generated using a deterministic process. This function is commonly used in programming to introduce variability and unpredictability.


What is meant by random number?

That is a number picked without any thought.


How do you get a random number from a group of numbers in C?

Random numbers cannot be generated programatically. For pseudo-random numbers use function 'rand'.


How do you hack a trackster code without having a trackster?

Try some random numbers and letters until you have one


Why random in matlab vary between 0 and 1?

Many programming languages do the same. Note that you can extend the range, simply by multiplying by some number. For example, if you want a random number between 0 and 10, multiply the random number by 10.


Is there such thing random numbers?

yes, because the number generated is from the computer's CPU database and is selected randomly therefore it must be a random number It depends on how the numbers are generated. If they are based on things in the environment, like noise levels, then yes. If they are solely generated by computer functions, then no, they are pseudo-random numbers, which will be random enough for most purposes.


What is the probability of getting two prime numbers 1-10?

The probability of getting two prime numbers when two numbers are selected at random and without replacement, from 1 to 10 is 2/15.