Bitcoin Forum
May 21, 2024, 01:35:29 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: generate random number  (Read 106 times)
Kostelooscoin (OP)
Member
**
Offline Offline

Activity: 202
Merit: 16


View Profile
February 17, 2023, 12:43:28 PM
 #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 <module>
    elements.remove(selection)
AttributeError: 'str' object has no attribute 'remove'
ABCbits
Legendary
*
Offline Offline

Activity: 2884
Merit: 7509


Crypto Swap Exchange


View Profile
February 17, 2023, 01:00:38 PM
Merited by hosseinimr93 (4)
 #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]

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
Kostelooscoin (OP)
Member
**
Offline Offline

Activity: 202
Merit: 16


View Profile
February 17, 2023, 01:05:15 PM
 #3

perfect thank you very much  Wink
witcher_sense
Legendary
*
Offline Offline

Activity: 2352
Merit: 4369


🔐BitcoinMessage.Tools🔑


View Profile WWW
February 17, 2023, 01:07:56 PM
 #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().


█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
Kostelooscoin (OP)
Member
**
Offline Offline

Activity: 202
Merit: 16


View Profile
February 18, 2023, 01:37:27 PM
 #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
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!