How to generate unique random number

edited July 2017 in How To...

How to generate unique random number from range 1 - 10.

Answers

  • Fill an array with those

    Shuffle the array

    Display the first number, then the 2nd number etc. from this array

  • edited July 2017

    I don't know how to type that syntax and If I need 5 numbers. How I should type ?

  • Answer ✓
    IntList il = IntList.fromRange(1, 11);
    il.shuffle(this);
    il.resize(5);
    println(il);
    exit();
    
  • edited July 2017 Answer ✓

    @alohaman.

    I need to do something with a list of Integers.

    ...only in a random order. "Shuffled."

    Start by using that example sketch right there in the reference.

    This is what to do if you are working with a list (like a deck of cards, with each card occurring once.

    7, 8, 2, 5, 1, 3, 0, 9, 4, 6

    If instead you were trying to but instead like rolling a 10-sided die (and each value is independent)

    7, 7, 6, 3, 0, 7, 2, 6, 8, 1

    ...then you can do that like this:

    int x = (int)random(10);
    println(x);
    
  • use (int)random(1, 11) to get integers from 1 to 10

  • edited July 2017

    Thanks everyone attend my post

    I use shuffle and list of integers. It can work to random 5 numbers. But how can I take those numbers to separate for use with others.

  • edited July 2017

    But how can I take those numbers to separate for use with others.

    I don't understand this sentence.

    Can you show your code, and explain exactly what you are trying to do?

    Is your problem this?:

    Q. "I made an IntList. How do I get a number from the IntList?"
    A. "Read the IntList reference! It shows an example."

    The entry has an example sketch that shows how to use IntList.get(), copy the value into an int, then draw it as text.

  • edited July 2017

    @jeremydouglass Oh sorry,to not answer you. Now I got a answer. "But how can I take those numbers to separate for use with others." that sentence ,I mean

    IntList il = IntList.fromRange(1, 11);
    il.shuffle(this);
    il.resize(5);
    println(il);
    exit();
    

    If I use that code, How can I separate them to be unique number as such as B[1] , B[2] , B[3] and B[1] = shuffle first number. B[2] = shuffle second number. B[3] = shuffle third number. and any number. for use with others function. I 'm so sorry from my english ,if u don't understand.

    and this is my code that I got a answer.

    import java.util.Random;
    int[] number = new int[5];
    int count=0;
    int num;
    Random r = new Random();
    while(count<number.length){
        num = r.nextInt(10);
        boolean repeat = false;
        do{ //1
            for(int i=0; i<number.length; i++){ //2
                if(num == number[i]){//3
                    repeat = true;
                    break;
                } //3
                else if(i == count){ 
                    number[count] = num;
                    count++;
                    repeat = true;
                    break;
                }
            } //2
        }while(!repeat); //1
    
    }
    
    for(int j = 0; j < number.length; j++){
        System.out.print(number[j]+" ");
    }
    

    Credit : https://stackoverflow.com/questions/33636887/generating-10-random-numbers-without-duplicate-with-fundamental-techniques

  • @alohaman --

    Read the IntList reference!

    Read the IntList reference!

    https://processing.org/reference/IntList.html

    The example at the top shows how to get a specific number -- using get()!

    The entry on get() right below says "get(): Get an entry at a particular index"

    Read the IntList reference!

  • @jeremydouglass hah, I got it. Thanks for repeat that for me.

Sign In or Register to comment.