Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: Kostelooscoin on February 17, 2023, 12:43:28 PM



Title: generate random number
Post by: Kostelooscoin on February 17, 2023, 12:43:28 PM
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 <module>
    elements.remove(selection)
AttributeError: 'str' object has no attribute 'remove'


Title: Re: generate random number
Post by: ABCbits on February 17, 2023, 01:00:38 PM
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]


Title: Re: generate random number
Post by: Kostelooscoin on February 17, 2023, 01:05:15 PM
perfect thank you very much  ;)


Title: Re: generate random number
Post by: witcher_sense on February 17, 2023, 01:07:56 PM
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().



Title: Re: generate random number
Post by: Kostelooscoin on February 18, 2023, 01:37:27 PM
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