0

how to print 100 random numbers of "set" in python, means I have to take 100 random numbers from given range and add it to an empty set(). I need solution in Python.I have tried in following way but its not taking 100 numbers exact.

import random
s=set()
for i in range(200):
    s.add((random.randint(0,101)))
print(s)
print(len(s))
DavidG
  • 24,279
  • 14
  • 89
  • 82
Kiran Patil
  • 31
  • 2
  • 6
  • 1
    A set cannot contain duplicate entries. This might help - https://stackoverflow.com/questions/12354515/what-is-the-difference-between-sets-and-lists-in-python – DavidG May 09 '18 at 08:11
  • 1
    Change `for i in range(200):` to `while len(s) < 100:` – ayhan May 09 '18 at 08:11
  • You can't add numbers to a set that are already contained, Try to check if a number is already contained with `if i in s` before adding. – meissner_ May 09 '18 at 08:12
  • 1
    I didn't understand, you want a set that contains 100 random items in the range of (0, 100) ? That means you want all of the numbers from 0 to 100. – BcK May 09 '18 at 08:12
  • I have given a limit while adding to set, 200 is given for to take 100 random numbers from 200. – Kiran Patil May 09 '18 at 08:13
  • Your call to `randint()` seems to be generating numbers in the range 0..101 inclusive. That gives you only 102 possible numbers. You would be better off generating a list containing 0..101, then randomly removing two. – quamrana May 09 '18 at 08:15
  • 3
    `random.sample(range(200), 100)` will generate 100 unique numbers from the range `[0,200)`. – Aechlys May 09 '18 at 08:17
  • if I changes to, while len(s) < 100 it takes all 100 numbers in sequential manner, but I want random numbers i.e. in non sequential manner @user2285236 – Kiran Patil May 09 '18 at 08:21
  • `len(s)` checks the the number of elements in the set `s`. It has nothing to the with how the numbers are selected. Translated to plain English: "until set contains 100 numbers" – Aechlys May 09 '18 at 08:24

4 Answers4

8

This will create a set that always has 100 elements (given the input range is equal to or larger than 100).

import random
set(random.sample(range(1337), 100))
vidstige
  • 12,492
  • 9
  • 66
  • 110
0

as comments said the set can't contain duplicated numbers, so you need to execute a while loop until you get the number of elements you need in your set.

Then add a random number

import random
s=set()
while len(s) < 100:
    s.add((random.randint(0,200)))
print(s)
print(len(s))
Chopi
  • 1,123
  • 1
  • 12
  • 23
0

sets don't allow duplicate values (that's part of sets defininition...), and statically you will get duplicate values when calling random.randint(). The solution here is obviously to use a while loop:

while len(s) < 100:
    s.add(random.randint(0, 101))

Note that with those values (100 ints in the 0..101 range) you won't get much variations since you're selecting 100 distinct values out of 102.

Also note that - as quamrana rightly mentions in a comment - if the range of possible values (randint() arguments) is smaller than the expected set length, the loop will never terminate.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • FYI. using a `while` with random numbers like this leaves open the possibility of the loop never terminating. – quamrana May 09 '18 at 08:31
0

set() can only contain unique items. If you try adding an item to set() that already exists, it will be silently ignored:

>>> s = set()
>>> s.add(1)
>>> s
{1}
>>> s.add(1)
>>> s
{1}

In your original code you're actually trying to "Add 200 random numbers to a set from 0 to 100". Conceptually this is wrong, because it's not possible to get 200 unique random numbers from a range of 0 - 100. You can only get up to 100 unique random numbers from that range.

The other issue with your code is what you're randomly choosing the number in each iteration, without checking if it has been added before.

So, in order to take N random numbers from a range of 0 to M, you would have to do the following:

import random  

s = set()

N = 100   # Number of items that will be appended to the set
M = 200   # Maximum random number

random_candidates = list(range(M))

for _ in range(N):
    numbers_left = len(random_candidates)
    # Choose a random number and remove it from the candidate list
    number = random_candidates.pop(random.randrange(numbers_left)) 
    s.add(number)

The above will work well for small ranges. If you expect M to be a large number, then generating a large random_candidates array will not be very memory effective.

In that case it would be better to randomly generate a number in a loop until you find one that was not chosen before:

import random

s = set()

N = 100   # Number of items that will be appended to the set
M = 2000  # Maximum random number    

for _ in range(N):
    random_candidate = random.randrange(M)
    while random_candidate in s:
        random_candidate = random.randrange(M)
    s.add(random_candidate)
samu
  • 2,870
  • 16
  • 28
  • Seems like an overly complicated and costly way to solve the problem. Just adding random numbers to `s` until it reaches length 100 is way simpler. – bruno desthuilliers May 09 '18 at 11:07