Python Forum
Random nr. no repetition & printing multiple lines
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random nr. no repetition & printing multiple lines
#1
First of all i apologize if a make any grammar mistakes English is not my native language.
I'm new to python and i have the following dilemma:
How do i print a random number set let's say 30 characters long without any repetitions.
This is the code that i have:
import random

mylist = []

for i in range(30):
    x = random.randint (1,100)
    if x not in mylist: mylist.append(x)

print (mylist)
This prints only one line and i want it to print 30, but the code also removes the duplicates and i end up with 27 or 26 numbers.
Hope you can help.
Thank you
Reply
#2
change line 9 to select each item (in loop) and print the item:
example
for item in items:
    print(item)
Reply
#3
There is random.sample(population, k) which "chooses k unique random elements from a population sequence or set." (use help(random.sample) for more information, press Q to quit help).

To get 30 unique items from range just:

random.sample(range(1, 100), 30)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Thank you for your responses.

Regarding what Larz60+ posted iv tried the code and it ended up giving 30 lines with the same numbers, i want the lines to differ.

Also i want to have the possibility to set the number of output lines.

Thank you
Reply
#5
what I showed was an example,
you needed to replace with your list name:
for item in mylist:
    print(item)
Reply
#6
Ok the changes you suggested:

import random
 
mylist = []
 
for i in range(10):
    x = random.randint (1,30)
    if x not in mylist: mylist.append(x)
 
for x in mylist:
    print(mylist)
This is the output:
[16, 4, 11, 15, 13, 14, 30, 18, 1]
[16, 4, 11, 15, 13, 14, 30, 18, 1]
[16, 4, 11, 15, 13, 14, 30, 18, 1]
[16, 4, 11, 15, 13, 14, 30, 18, 1]
[16, 4, 11, 15, 13, 14, 30, 18, 1]
[16, 4, 11, 15, 13, 14, 30, 18, 1]
[16, 4, 11, 15, 13, 14, 30, 18, 1]
[16, 4, 11, 15, 13, 14, 30, 18, 1]
[16, 4, 11, 15, 13, 14, 30, 18, 1]

Is there something that i miss?
Reply
#7
Still - why not sample?

>>> import random
>>> random.seed(42)                                     # for replicating result, don't use it in actual code
>>> print(*random.sample(range(1, 100), 5), sep='\n')
82
15
4
95
36
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
You're still printing out the entire list each time!

should be:
>>> import random
>>> mylist = []
>>> for i in range(10):
...     x = random.randint (1,30)
...     if x not in mylist: mylist.append(x)
... 
>>> mylist
[15, 10, 3, 4, 25, 28, 27, 7]
>>> 
>>> for x in mylist:
...     print(x)
... 
15
10
3
4
25
28
27
7
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write the condition for deleting multiple lines? Lky 3 1,160 Jul-10-2022, 02:28 PM
Last Post: Lky
  Delete multiple lines from txt file Lky 6 2,321 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  python-docx: preserve formatting when printing lines Tmagpy 4 2,120 Jul-09-2022, 01:15 AM
Last Post: Tmagpy
  Display table field on multiple lines, 'wordwrap' 3python 0 1,776 Aug-06-2021, 08:17 PM
Last Post: 3python
  pulling multiple lines from a txt IceJJFish69 3 2,596 Apr-26-2021, 05:56 PM
Last Post: snippsat
  Way to avoid repetition? Tuxedo 5 2,885 Feb-16-2021, 08:02 PM
Last Post: Tuxedo
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,885 Aug-10-2020, 11:01 PM
Last Post: medatib531
  question about you want repetition this task loczeq 6 3,371 Mar-05-2020, 08:35 PM
Last Post: loczeq
  print python json dump onto multiple lines lhailey 2 19,900 Mar-02-2020, 12:47 PM
Last Post: vishalhule
  how to insert # to multiple lines? hkfatasy 1 2,912 Dec-22-2019, 01:51 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020