#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# How to generate unique random number

edited July 2017

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

Tagged:

• Fill an array with those

Shuffle the array

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

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

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

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

• 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.

• 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?

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.

• @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 , B , B and B = shuffle first number. B = shuffle second number. B = 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;
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]+" ");
}
``````