1

How can I generate a list of 10 random integer numbers in the range between 0 and 100 so that all random numbers are unequal?

mrz
  • 11,686
  • 2
  • 25
  • 81
  • 3
    and http://mathematica.stackexchange.com/questions/7926/randomly-selecting-elements-without-repetition – Young Jul 12 '16 at 14:32

2 Answers2

10
RandomSample[Range[0, 100], 10]
vapor
  • 7,911
  • 2
  • 22
  • 55
  • I was to much focused on RandomInteger, thanks. – mrz Jul 12 '16 at 14:33
  • @happyfish if you saw my comment ... I'm sorry, that was unprofessional – Young Jul 12 '16 at 16:05
  • @Young I didn't, but I am curious now :P – vapor Jul 12 '16 at 16:09
  • It was a sarcastic remark about answering a question that was bound to have been asked previously and collecting 95 rep. It was uncalled for and not my place. – Young Jul 12 '16 at 16:12
  • 3
    @Young I am also surprised by the votes and I agree with you. I feel guilty since I have almost never posted an answer that requires real effort in this community. Perhaps because as a beginner, my knowledge is a subset of many active users. It will still take me sometime to learn before I can make real contributions. – vapor Jul 12 '16 at 16:21
  • 3
    @Young I'm of the opinion that for a lot of duplicate questions, especially from first time posters (not that that applies here), it's often better to answer and let others worry about marking duplicates. I feel like it may encourage them to come back more, and our community here is not so large as to discourage newbies – Jason B. Jul 13 '16 at 01:14
5

happy fish gave the right answer, but sometimes it's fun to go oldschool. You can use rejection sampling and a While loop

list = {};
While[Length@list <= 10, 
 list = DeleteDuplicates@Append[list, RandomInteger[100]]]
rcollyer
  • 33,976
  • 7
  • 92
  • 191
Jason B.
  • 68,381
  • 3
  • 139
  • 286
  • I like this approach as you don't need to generate the full list. A small modification might save a lot of iterations: While[(l = Length[ids]) < n, ids = DeleteDuplicates[Join[ids, RandomInteger[max, n - l]]]] – Batracos Jan 27 '21 at 09:11