0

I just started to learn programming .As I proceed chapters to chapters ,I start encountering problem .One of the biggest problem is when I face a question in generating random numbers with python."Generate 20 random unique numbers and sort them in an order whether in ascending order or descending order".I have done a research on how can I do it,but I am so confused right now and think of giving up.Can anyone give me an example about how can I do this and explanation for this.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
cklai
  • 41
  • 1
  • 1
  • 10
  • 1
    Which part don't you understand? Generating random numbers or putting them in a list or sorting ? – Anand S Kumar Jul 27 '15 at 08:28
  • I think OP is confused with the task itself. @Ellos just to clarify - you don't have to generate sorted random numbers in one go, you can generate a list of items and then sort them. – konart Jul 27 '15 at 08:29
  • Did you really research? This is almost a duplicate of [how do i create a LIST of unique random numbers?](http://stackoverflow.com/questions/9755538/how-do-i-create-a-list-of-unique-random-numbers). Look at the answers there. – Mario Zannone Jul 27 '15 at 08:33
  • possible duplicate of [Generating non-repeating random numbers in Python](http://stackoverflow.com/questions/2076838/generating-non-repeating-random-numbers-in-python) – Clodion Jul 27 '15 at 08:35

4 Answers4

5
>>> import random
>>> var = random.sample(range(1, 10000), 20)
>>> var
[4691, 1789, 9473, 4042, 8423, 5021, 2627, 2739, 6337, 4963, 5772, 9180, 2788, 1197, 1276, 3393, 7748, 9448, 3618, 1835]
>>> var.sort()
>>> var
[1197, 1276, 1789, 1835, 2627, 2739, 2788, 3393, 3618, 4042, 4691, 4963, 5021, 5772, 6337, 7748, 8423, 9180, 9448, 9473]

First you generate 20 numbers from given range and then just simple sorting it with sort()

Morishiri
  • 874
  • 10
  • 23
  • random.sample(population, k) Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement. [reference](https://docs.python.org/2/library/random.html) – Morishiri Jul 27 '15 at 08:58
  • 2
    @Clodion : [`random.sample()`](https://docs.python.org/2/library/random.html#random.sample) returns duplicates only if there are duplicates in the given population. There are no duplicates in `range(1,10000)`, so there will not be any duplicates returned. But for e.g. `random.sample([1]*20, 10)` will return duplicates. – mhawke Jul 27 '15 at 09:03
0

You can use random module:

>>> from random import random
>>> lst = sorted([random() for x in range(20)])

Response:

[0.023779304268199186, 0.09302269288440945, 0.09832361922809707, 0.25242017515656756, 0.3506703186883351, 0.35105790921933866, 0.36493338390569785, 0.4758485640689768, 0.5224413002068954, 0.5794797893890528, 0.5826667916581931, 0.6050065111356167, 0.6283016356471698, 0.6709347774971688, 0.680474965216515, 0.6871898214051998, 0.7105349504105246, 0.7362770892874505, 0.8778691065354278, 0.8980497474361055]

Or, if you want to be sure that there is no duplicate:

from random import randint
a_set = set()
while True:
    a_set.add(randint(0, 1000))
    if len(a_set)==20:
        break
lst = sorted(list(a_set))
print(lst)

Result:

[6, 94, 132, 258, 370, 389, 407, 462, 468, 528, 637, 640, 677, 692, 704, 872, 875, 883, 889, 913, 999]
Clodion
  • 1,017
  • 6
  • 12
0

Try the following code:

import random
startValue = 5
endValue = 150
rangeOfNumbers = xrange(startValue, (endValue + 1))
count = 10
randomNumList = random.sample(rangeOfNumbers, count)
print "Unsorted list of random numbers", randomNumList
randomNumList.sort()
print "Sorted list of random numbers", randomNumList

You will get the following output:

Unsorted list of random numbers [131, 138, 137, 22, 23, 69, 21, 66, 12, 83]
Sorted list of random numbers [12, 21, 22, 23, 66, 69, 83, 131, 137, 138]
abcdef
  • 137
  • 1
  • 8
-1
import random


def make_list_distinct(_l, wanted_lenght=20):
    current_list = list(set(_l))
    _ = 20 - len(current_list)
    if _ > 0:
        current_list = current_list + [random.randint(1, 2000) for i in range(_)]
        make_list_distinct(_l = current_list)
    else:
        final_list = sorted(current_list)
        print(final_list)

if __name__ == '__main__':

    #a = [random.randint(1, 2000) for i in range(20)]
    a = [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
    make_list_distinct(a)
Sinux
  • 1,728
  • 3
  • 15
  • 28
  • And what if you get duplicate number(s) in the second list comprehension? – mhawke Jul 27 '15 at 08:37
  • Still not quite correct. You might get only 18 distinct random numbers the first time. (Unlikely, but possible.) You should change the `if i > 0` to `while i > 0` and then recompute i within the while loop. – saulspatz Jul 27 '15 at 08:49
  • This _might_ work, but it is not nice at all. Just use `random.sample()`. – mhawke Jul 27 '15 at 08:50
  • @mhawke. Of course, but I was critiquing the technique, not providing a recipe. – saulspatz Jul 27 '15 at 08:51
  • @saulspatz I've tested 18 duplicated numbers for the unlikely situation. – Sinux Jul 27 '15 at 09:01