I don't know your doubt/angle for this problem.
If you just want to solve, use numpy:
import numpy as np
# 12345 is a random_state (allows reproducibility)
# remove if you want to generate different numbers
# from different iterations
rng = np.random.default_rng(12345)
rints = rng.choice(np.arange(1, 100), 10, replace=False)
rints
Considering a student perspective:
- Your code do this:
for 10 runs:
pick one random integer from 1 to 99
if the integer is not in list
append it
else
do nothing
#That's why you're getting 8 or 9 numbers from some runs.
You need to "compensate" when you draw a repeated integer:
import random
my_list = []
n = 0
iters = 1
while n < 10:
r=random.randint(1,100)
if r not in my_list:
my_list.append(r)
print(my_list)
print(len(my_list))
print(iters)
n += 1
iters += 1
- You shouldn't use "list" to assign variables. Some words are reserved in python [1]. Being more specific, "list" isn't a keyword, just a build-in function [2], but assign a value for it can be considered an anti-pattern [3].
[1] https://docs.python.org/3/reference/lexical_analysis.html#keywords
[2] https://docs.python.org/3/library/functions.html#func-list
[3] https://frostyx.fedorapeople.org/The-Little-Book-of-Python-Anti-Patterns-1.0.pdf