So i was wondering how to pick a random number from 1 to 100, then again repeat and pick another random number, and keep repeating until i have 10 random numbers. How to make sure none of the random numbers repeat? So that they are all unique numbers.
How to pick a random number from 1 to 100? Repeat this 10 times and make sure no number is repeated?
-
2Each time you get a new number, treat it as a candidate. Compare it to each of the previously obtained ones. If the candidate is equal to any of them, discard it and go for a new number. Otherwise, accept it as a valid member of your set of unique randomly obtained numbers. – Daniel Daranas Mar 26 '14 at 17:32
-
3Create an array of integers from 1 to 100 and shuffle it into a random order, then use the values in 0, 1, etc order. – bitfiddler Mar 26 '14 at 17:33
-
depending on the language you are using, there may be a library that can give you a unique distribution of random numbers – Red Alert Mar 26 '14 at 17:34
-
http://stackoverflow.com/a/693919/2864740 – user2864740 Mar 26 '14 at 17:39
-
Thanks a lot for the replies. The link to the other thread proved really useful! Great. – AspiringDeveloper Mar 26 '14 at 17:41
6 Answers
Take the numbers 1–100. Shuffle them randomly. Return the first 10.
This is a variant of the Fisher-Yates shuffle, for a zero-based array a of length n:
for i from n − 1 downto 1 do
j ← random integer with 0 ≤ j ≤ i
exchange a[j] and a[i]

- 265,237
- 58
- 395
- 493
-
Could you please explain how to shuffle such an array of 100 numbers – AspiringDeveloper Mar 26 '14 at 17:39
-
@AspiringDeveloper The [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) is a widely accepted method. I'll add more to my answer. – erickson Mar 26 '14 at 17:40
-
The simplest approach is to generate the next number in a loop, and exit the loop when the newly generated number is not already picked. Pseudocode:
Generated = empty-sequence
repeat 10 times:
| repeat:
| | New = uniform-random-int [1, 100]
| until New not in Generated
| append New to Generated
There are faster ways if you need speed, but that's a start.

- 8,546
- 3
- 29
- 49
-
While it's an obvious approach, I don't think this should be mentioned as anything but a negative example since there is no bound on the time complexity of this. – G. Bach Mar 26 '14 at 18:05
-
@G. Bach: On the contrary, this one is rather fast actually. If we have to generate all N numbers from the range [1..N], the expected number of calls to `uniform-random-int` is N/N + N/(N-1) + N/(N-2) + ... + N/2 + N/1 ~ N log N. True, it is not bounded due to randomness, but for N > 10, if your random number generator makes, say, double the number of expected calls, you have much bigger problems with the generator itself than with the algorithm. Actually, this algorithm is also faster than an O(N) approach when we have to generate much less than N numbers. – Gassa Mar 26 '14 at 18:21
My opinion is that if you want to be sure that you get 10 random numbers, once you generate one random number put it in a list or something similar and for other ones just check if it is in the list, if it isn't insert it there if it is generate another one. Keep doing this until you have 10 or how many do you need.

- 1,166
- 12
- 23
Well I don't know which program or thing you were going to use, but I would do it like this:
1 create a random number.
2 store the number in an array or row.
3 Then create a new number.
4 Check that number, if it is already used, then delete the number and try again.
5 else store that number in the same array or row (number 2)
6 repeat step 3-5 untill you have 10 unique numbers.
Hope this helps!
In Java for example:
Random rand = new Random();
HashSet<Integer> randoms = new HashSet();
while(randoms.size() < 10) {
randoms.add(rand.nextInt(100));
}
-
To improve your answer, you should explain what your code is doing. Especially since this question is focused on the how the algorithm works. – Kirk Backus Mar 26 '14 at 18:19
Simplest algorithm can be assuming you have no space issue (pseudo-code)
list = [1,2,3,4,5,.....100]
randomly shuffle the list using fishers Yates shuffle or any other shuffle algorithm
use the first 10 digits
A probable code in python could be
import random
list1=[]
for i in range(1,101):
list1.append(i)
random.shuffle(list1)
for i in range(0,10):
print(list1[i])
Fisher Yates shuffle is a simple algorithm Let's say you have an array of N elements,sat A[N] the index being [0, N-1].
- Start iterating the array from the last element.
- If you are at ith element from the end, generate a random number say j,in range[0,i].
- Swap A[i] and A[j].
- Continue till you reach the element at index 1 i.e A1.
so in python to shuffle 10 values you can go like
import random
a=[1,2,3,4,5,6,7,8,9,10] # initializing the values to be sorted in a matrix
index=9
while(index>=0):
x=random.randint(0,index)
a[x],a[index]=a[index],a[x] #swapping values at A[x] with A[index]
index-=1 #reducing the value of index by 1
print (a)

- 1
- 1

- 1,415
- 3
- 16
- 30