Thomas.Chan
Thomas.Chan

Reputation: 31

How to make my random number unique and sort the number

I use collection shuffle but it doesn't work so far I go with this.

import java.util.Random;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class TotoMachine {
    public static void main(String[] args) {
        int[] array = new int[7];
        Random rand = new Random();
        for (int i = 0; i < array.length; i++)
            array[i] = rand.nextInt(65) + 1;
        //Collections.shuffle()
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
}

Upvotes: 0

Views: 181

Answers (7)

Timetrax
Timetrax

Reputation: 1493

if not using a set: You might do something like this.

Generate random number.

Check array to see if random number exists

If not exists -- insert random number into array.

If exists generate new random number and repeat the above steps.

Do this until your array is populated to capacity.

This method can be made alot sweeter with recursion.

Otherwise use sets: sets do not contain any duplicates.

You can then convert your set to an array as some examples show.

Upvotes: 0

user4910279
user4910279

Reputation:

Try this.

public static void main(String[] args) {
    Set<Integer> set = new TreeSet<>();
    Random rand = new Random();
    while (set.size() < 7)
        set.add(rand.nextInt(65) + 1);
    System.out.println(set);
}

Upvotes: 1

Oghli
Oghli

Reputation: 2341

You can use Set data structure to make unique random numbers in this way:

Add every new random elements to the Set then you only add it to the array if it's unique element.

public static void main(String[] args) {
    int[] array = new int[7];
    int randomNum;
    Set<Integer> numbers = new HashSet<>(); //Set of unique elements
    Random rand = new Random();
    for (int i = 0; i < array.length; i++){
        randomNum = rand.nextInt(65) + 1;
        numbers.add(randomNum);
        if(!numbers.contains(randomNum)){ // if unique element add it to array
            array[i] = randomNum;
        }
        else{    // keep generating new random numbers until you get unique element
            while(numbers.contains(randomNum)){
                randomNum = rand.nextInt(65) + 1;
            }
            numbers.add(randomNum);
            array[i] = randomNum;
        }
     }
    Arrays.sort(array);
    System.out.println(Arrays.toString(array));            
}

I hope this would solve your problem.

Upvotes: 0

Sagar Gautam
Sagar Gautam

Reputation: 9389

This might work: Add random number generated to a Set untill some size. Then convert it to Array like this:

import java.util.*;

class random{
    public static void main(String[] args){
    int max=65;
    int min=0;
    int size=7;

    Set<Integer> set = new HashSet<Integer>();
    Random random = new Random(); 
    while(set.size()<size){
        int randomNum = random.nextInt((max - min) + 1) + min;
        set.add(randomNum);
    }
    Integer[] intArray= set.toArray(new Integer[set.size()]);
    Arrays.sort(intArray);
    for(int i=0;i<size;i++){
        System.out.println(intArray[i]+" ");    
    }
    }
}

Upvotes: 0

Rajith Pemabandu
Rajith Pemabandu

Reputation: 614

suppose you can try like this as a work around. Hope you are familiar with List.

public static void main(String[] args){
           List<Integer> array = new ArrayList<Integer>();
           Random rand = new Random();
           int num;
           for (int i = 0; array.size() < 7 ; i++){

               num = rand.nextInt(65) + 1;
               if(!array.contains(num)){
                   array.add(num);
               }
           }

           Collections.sort(array);
           System.out.println(array.toString());
           }    
}

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201527

I suggest you use Collections, specifically Set(s). You might create a LinkedHashSet<Integer> and add your desired number of random values. Then you might add those values to a TreeSet<Integer> (which is sorted). Something like,

Random rand = new Random();
final int SIZE = 7;
Set<Integer> set = new LinkedHashSet<>(SIZE);
while (set.size() < SIZE) {
    set.add(rand.nextInt(65) + 1);
}
System.out.println(set);
Set<Integer> sortedSet = new TreeSet<>(set);
System.out.println(sortedSet);

or you could use streams and a Collector like

List<Integer> list = set.stream().sorted().collect(Collectors.toList());
System.out.println(list);

Upvotes: 0

SilverNak
SilverNak

Reputation: 3381

You could use a Set to achieve unique numbers. For example you could try this:

import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class TotoMachine {
    public static void main(String[] args) {
        int[] array = new int[7];
        Random rand = new Random();
        Set<Integer> set = new HashSet<>();
        while (set.size() < array.length) {
            set.add(rand.nextInt(65) + 1);
        }
        int i = 0;
        for (Integer integer : set) {
            array[i] = integer;
            i++;
        }
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
}

If you have any chance to use the already created set, do so instead of converting it to an int[] array. If you are free to use an Integer[] instead of an int[], you could convert it like this:

Integer[] arr = new Integer[7];
set.toArray(arr);

Upvotes: 0

Related Questions