I wanted to know if there is a function like that can choose from some values randomly like if i give the function these values 837,475,253,585 it will choose one randomely like if it chooses 253 randomely i wanted to know if there is a function like that i searched on google but it didnt show me much (BTW I AM TALKING ABOUT PYTHON)
            Asked
            
        
        
            Active
            
        
            Viewed 39 times
        
    2 Answers
0
            
            
        You can use random library and use the choice option and set those values as options
import random
mylist = ["apple", "banana", "cherry"]
print(random.choice(mylist))
 
    
    
        NodeX4
        
- 150
- 1
- 10
- 
                    2`random` is a builtin module in pyhton, you don't need to install it. – Jhanzaib Humayun May 01 '22 at 05:42
- 
                    2
0
            
            
        There is such a function as mentioned here - random.choice(). It is built-in Python library
We have an numpy alternative, which is more optimized for these types of operations. It need to be installed by pip install numpy
import numpy as np
rand_choice = np.random.choice(a = 5, size = 5, replace = True, p = weights)
 
    
    
        MegaKarg
        
- 110
- 1
- 7
