Author

Topic: generate random number (Read 133 times)

member
Activity: 206
Merit: 16
February 18, 2023, 08:37:27 AM
#5
Code:
from random import shuffle

numbers = list(range(100))
shuffle(numbers)
while numbers:
    number = numbers.pop()
    print(number)

1) Create a list of numbers from 0 to 99
2) Shuffle them with one of functions from random module
3) Pop the last item from the list until it is empty


Please note that random uses pseudorandom algorithm, for any serious randomization use secrets module or os.urandom().



without 0
legendary
Activity: 2464
Merit: 4415
🔐BitcoinMessage.Tools🔑
February 17, 2023, 08:07:56 AM
#4
Code:
from random import shuffle

numbers = list(range(100))
shuffle(numbers)
while numbers:
    number = numbers.pop()
    print(number)

1) Create a list of numbers from 0 to 99
2) Shuffle them with one of functions from random module
3) Pop the last item from the list until it is empty


Please note that random uses pseudorandom algorithm, for any serious randomization use secrets module or os.urandom().

member
Activity: 206
Merit: 16
February 17, 2023, 08:05:15 AM
#3
perfect thank you very much  Wink
legendary
Activity: 2870
Merit: 7490
Crypto Swap Exchange
February 17, 2023, 08:00:38 AM
#2
I don't know what is your main goal. But i did quick change to your code and this should do what you want.

Code:
import random                                       

elements = []

while len(elements) < 100:
    selection =  random.randint(1, 100)
    print(selection)
    if selection not in elements:
        elements.append(selection)
    print(elements)

Here's snippet of the output,

Code:
22
[22]
14
[22, 14]
...
76
[22, 14, 4, 45, 39, 51, 36, 30, 94, 41, 73, 32, 89, 100, 64, 18, 6, 16, 46, 52, 98, 71, 58, 62, 33, 17, 29, 56, 9, 25, 83, 53, 28, 72, 37, 86, 31, 61, 15, 26, 99, 70, 84, 66, 93, 79, 48, 34, 78, 90, 97, 49, 75, 54, 55, 13, 85, 20, 7, 96, 81, 68, 57, 60, 74, 11, 91, 69, 1, 40, 65, 21, 12, 63, 87, 5, 92, 77, 24, 44, 67, 3, 8, 35, 50, 42, 38, 23, 10, 76, 95, 2, 59, 27, 47, 82, 88, 19, 43]
80
[22, 14, 4, 45, 39, 51, 36, 30, 94, 41, 73, 32, 89, 100, 64, 18, 6, 16, 46, 52, 98, 71, 58, 62, 33, 17, 29, 56, 9, 25, 83, 53, 28, 72, 37, 86, 31, 61, 15, 26, 99, 70, 84, 66, 93, 79, 48, 34, 78, 90, 97, 49, 75, 54, 55, 13, 85, 20, 7, 96, 81, 68, 57, 60, 74, 11, 91, 69, 1, 40, 65, 21, 12, 63, 87, 5, 92, 77, 24, 44, 67, 3, 8, 35, 50, 42, 38, 23, 10, 76, 95, 2, 59, 27, 47, 82, 88, 19, 43, 80]
member
Activity: 206
Merit: 16
February 17, 2023, 07:43:28 AM
#1
hi possible to generate a number by deleting what has already been generated in python without list

try :

Code:
import random                                      

elements = str(random.randint(1, 100))

i = 0
while i != 100:
        selection = elements
        
        print(elements)
        print(selection)
        i = i + 1
        elements.remove(selection)

result File "1.py", line 12, in
    elements.remove(selection)
AttributeError: 'str' object has no attribute 'remove'
Jump to: