1
$\begingroup$

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?

$\endgroup$
1

2 Answers 2

10
$\begingroup$
RandomSample[Range[0, 100], 10]
$\endgroup$
6
  • $\begingroup$ I was to much focused on RandomInteger, thanks. $\endgroup$
    – mrz
    Jul 12, 2016 at 14:33
  • $\begingroup$ @happyfish if you saw my comment ... I'm sorry, that was unprofessional $\endgroup$
    – Young
    Jul 12, 2016 at 16:05
  • $\begingroup$ @Young I didn't, but I am curious now :P $\endgroup$
    – vapor
    Jul 12, 2016 at 16:09
  • 3
    $\begingroup$ @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. $\endgroup$
    – vapor
    Jul 12, 2016 at 16:21
  • 3
    $\begingroup$ @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 $\endgroup$
    – Jason B.
    Jul 13, 2016 at 1:14
5
$\begingroup$

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]]]
$\endgroup$
1
  • $\begingroup$ 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]]]] $\endgroup$
    – Batracos
    Jan 27, 2021 at 9:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.