Reputation: 33
Write a program to generate any number of random integers in 0 to 100 range. Your program should get the size as a parameter and return the numbers as an array.
This the question I got and below is the code I tried, but I get duplicates with this code. How to generate random numbers without any duplicates?
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("size:\t");
int n = sc.nextInt();
int[] arr = new int[n];
Random rand = new Random(); //instance of a random class.
int upperbound = 101; //generate random values from 0-100
for(int i=0;i<n;i++) {
arr[i] = rand.nextInt(upperbound);
}
System.out.println("Random Numbers: "+ Arrays.toString(arr));
}
}
Upvotes: 2
Views: 4517
Reputation: 39
Instead of using an Array you could use an ArrayList and add the value to the ArrayList only if it has not been used before.
ArrayList<Integer> numbers = new ArrayList<Integer>();
Random random = new Random();
if (!numbers.contains(randomNumber)) {
numbers.add(randomNumber);
}
After this you could directly the print the ArrayList or convert it into an array and then print it out.
Integer[] arr = new Integer[number.size()];
// ArrayList to Array Conversion
for (int i = 0; i < number.size(); i++) {
arr[i] = number.get(i);
Upvotes: 0
Reputation: 106
this code work as well
public void solution(int size){
int upperbound = 101;
int arr[]=new int[upperbound];
/**
* initial a number arr store 0-100
*/
for (int i = 0; i <upperbound ; i++) {
arr[i]=i;
}
SecureRandom secureRandom=new SecureRandom();
for (int i = 0; i <size ; i++) {
/**
* if generate a index, print number and swap now index number and tail
*/
int index=secureRandom.nextInt(upperbound-i);
int tmp=arr[index];
int last=arr[arr.length-1-i];
System.out.println(tmp);
arr[index]=last;
}
}
Upvotes: 1
Reputation: 1178
You can create a list that contains all values from 0 to 100, then shuffle it and extract the first n elements of that list like this:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("size:\t");
int n = sc.nextInt();
int bound = 100;
List<Integer> items = IntStream.range(0, bound + 1)
.boxed()
.collect(Collectors.toList());
Collections.shuffle(items);
int[] arr = new int[Math.min(n, bound + 1)];
for(int i = 0; i < arr.length; i++){
arr[i] = items.get(i);
}
System.out.println("Random Numbers: "+ Arrays.toString(arr));
}
Upvotes: 3
Reputation: 909
You can use a HashSet
and keep adding all the new entries to it and while generating a new random
integer, check if it was already generated (if the number is present in the unique
set).
If the number was already generated, then keep generating new number until a unique number is found.
Set<Integer> unique = new HashSet<Integer>();
for (int i=0; i<100; i++)
{
int number = rand.nextInt(upperbound);
while (unique.contains(number)) // `number` is a duplicate
number = rand.nextInt(upperbound);
arr[i] = number;
unique.add(number); // adding to the set
}
Upvotes: 1
Reputation: 790
You can create a list of 1...100 (let's call in "l") , and an array of size 100 (let's call it "a").
Then, create a for loop and generate a number between 0 to l.size() - 1.
First iteration generation (let's call it index0) will be the index in l that will be taken as the first object in the array. So call l.remove(index0), and use the retrieved object in a[0].
Second iteration generation, between 0 to l.size()-1, which means 0 to 98. That will be index1. call l.remove(index1), the retrieved object will be used to the second index in a[1].
Do it until l is empty.
Upvotes: 1