I'd like to save 100 random numbers between 1-100 on the list and print them out 10 per line.
For example,
77 82 5 13 91 85 43 22 23 90
.
.
.
33 12 95 35 21 34 68 51 53 9
In the middle...has been omitted for convenience. Anyway, I want to arrange the numbers between 1-100 at random by 10 x 10. I've been trying to fix the code, but I'm not sure. Here's the last code I wrote.
import random
mynum = []
for i in range(100):
k = random.randint(1, 100)
while True:
k = random.randrange(1, 101)
if k not in mynum:
break
mynum.append(k)
for i in range(len(mynum)):
print("%3d"%i, end=' ')
if i%10 == 9:
print('\n')
I just started Python, so I'd appreciate it if you could explain it with basic functions as much as possible. Also, I want to know all the things that are wrong here.
python
Good job. At the end, you can only change the part that shows the random numbers you collect as follows.
for i, n in enumerate(mynum):
print("%3d"%n, end=' ')
if i%10 == 9:
print('\n')
In order not to pick what has already been picked again, the draw is repeated randomly again, and there is no problem with time when it is currently 100, but I think there may be a speed issue when it is a much larger number. If you say a million. When you have to pick one more, the last number is fixed, but in the code above, you randomly pick it again with a random function. So you have to repeat the one-in-a-millionth probability. You have to do it about a million times to pick the last number.
The way to improve it a little bit is to put the numbers that haven't been picked up in the set and pick them out one by one. In Python, there's also a function that if you're given a list or three, you pick one of them.
But the most efficient thing about this type of problem is to use what's called shuffle. Just mix the order like hwatu-pae. It is located in the random module.
>>> import random
>>> l = list(range(1, 101))
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
>>> random.shuffle(l)
>>> l
[61, 77, 64, 3, 83, 22, 100, 98, 60, 47, 79, 9, 46, 97, 45, 56, 58, 30, 71, 37, 4, 86, 68, 67, 27, 72, 31, 17, 29, 16, 70, 65, 24, 99, 76, 50, 92, 12, 95, 2, 25, 21, 88, 39, 23, 1, 96, 8, 93, 33, 63, 91, 7, 32, 36, 87, 42, 14, 80, 34, 66, 10, 62, 85, 73, 74, 40, 6, 41, 48, 28, 13, 53, 52, 35, 57, 38, 89, 69, 43, 26, 81, 44, 19, 82, 18, 55, 90, 75, 54, 15, 49, 20, 11, 51, 84, 5, 59, 94, 78]
>>> for i in range(10):
print(''.join(map(lambda n:'%3s '%n, l[10*i:10*(i+1)])))
61 77 64 3 83 22 100 98 60 47
79 9 46 97 45 56 58 30 71 37
4 86 68 67 27 72 31 17 29 16
70 65 24 99 76 50 92 12 95 2
25 21 88 39 23 1 96 8 93 33
63 91 7 32 36 87 42 14 80 34
66 10 62 85 73 74 40 6 41 48
28 13 53 52 35 57 38 89 69 43
26 81 44 19 82 18 55 90 75 54
15 49 20 11 51 84 5 59 94 78
>>> l = list(range(1, 101))
>>> r = []
>>> random.choice(l)
37
>>> while l:
c = random.choice(l) ##
r.append(c) ## Put it in the list of results
l.remove(c)## Delete from original list
>>> l
[]
>>> r
[65, 32, 15, 2, 45, 47, 11, 98, 97, 42, 36, 87, 40, 35, 55, 65, 12, 96, 74, 91, 48, 21, 54, 3, 1, 75, 16, 77, 82, 68, 67, 44, 51, 89, 90, 57, 20, 29, 85, 34, 63, 81, 27, 23, 28, 46, 49, 25, 66, 22, 69, 80, 70, 86, 59, 73, 19, 31, 71, 37, 62, 61, 72, 99, 60, 43, 84, 39, 52, 78, 17, 14, 64, 18, 13, 5, 4, 83, 93, 53, 92, 26, 33, 95, 76, 24, 30, 88, 56, 8, 9, 94, 58, 41, 50, 38, 6, 7, 100, 10, 79]
>>> for i in range(10):
print(''.join(map(lambda n:'%3s '%n, r[10*i:10*(i+1)])))
65 32 15 2 45 47 11 98 97 42
36 87 40 35 55 65 12 96 74 91
48 21 54 3 1 75 16 77 82 68
67 44 51 89 90 57 20 29 85 34
63 81 27 23 28 46 49 25 66 22
69 80 70 86 59 73 19 31 71 37
62 61 72 99 60 43 84 39 52 78
17 14 64 18 13 5 4 83 93 53
92 26 33 95 76 24 30 88 56 8
9 94 58 41 50 38 6 7 100 10
565 PHP ssh2_scp_send fails to send files as intended
572 Uncaught (inpromise) Error on Electron: An object could not be cloned
824 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
581 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.