Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

generate random list of number py

import random
# list of n elements, going between a and b
list = [random.randint(a,b) for _ in range(n)]
print("list:", list)
Comment

random list python

import random
#random numbers 0-9 in a list of length 10
array = [random.randint(0,9) for i in range(10)]
print(array)
Comment

Random List Python

import random

mylist = ["apple", "banana", "cherry"]
random.shuffle(mylist)

print(mylist)
Comment

python random generator from list

import random
n = random.random()
print(n)
Comment

random from list python

random.choice(seq)¶
Return a random element from the non-empty sequence seq.
Comment

python - Syntax to call random function from a list

my_list = [func_test_1, func_test_2, func_test_3]
random.choice(my_list)()
Comment

random list generator

"""Generation of unique random list of size n
"""
from random import sample
def unique_lst(n):
    return sample(range(10, 100), n) # return a sample of lst (unique lst)
    
# print(unique_lst(10))
Comment

random list generator

"""
Generation of non-unique 1d_lst
- Function takes in 3 parameters (that has been defaulted)
- If you are looking to generate unique lsts...
  ...you might want to use random.sample() on a lst of numbers...
  ...such as list(_ for _ in range(10))
"""

from random import randint
def nu_rand1d(r = 10, low = 11, high = 99): # r is range, nu means non-unique
    return [randint(low, high) for num in range(r)]

"""
- Note that randint is inclusive
  e.g. If x = randint(10, 100), 10 <= x <= 100
  
- If you prefer, you can use randrange which is exclusive like range()
  e.g. If x = randrange(10, 100),  10 <= x <= 99
"""
Comment

random list generator

"""Generation of non-unique 1d_lst and 2d_lst
- Function takes in a list of info about the non-unique list

- You can also import randint from random. 
  - Note that randint is inclusive and randrange is exclusive.
  
- If you are looking to generate unique lsts...
  ...you might want to use random.sample() on a lst of numbers.
"""

from random import randrange

dft = [10, 10, 100, 10] # dft = default (range, low, high, row)

def rand_1d(info = dft[:-1]):
    r, low, high = info # r: range
    return [randrange(low, high) for num in range(r)]

def rand_2d(info = dft):
    *info_1d, rows = info
    return [rand_1d(info_1d) for i in range(rows)]

## Test
randlst_gen = {
    "rand_1d": rand_1d,
    "rand_2d": rand_2d
}
r1, r2 = randlst_gen.values() # get the functions

# #function 1
# print("rand_1d --------------------------------------------------")
# print(f"default: {r1()}")
# print(f"rand: {r1([5, 3, 234])}")
# print()
# # function 2
# print("rand_2d --------------------------------------------------")
# print("default:")
# for row in r2(): 
#     print(row)
# print()
# print("rand:")
# for row in r2([8, 11, 30, 5]): 
#     print(row)
Comment

PREVIOUS NEXT
Code Example
Python :: mkvirtualenv python version 
Python :: change base python 
Python :: plot scatter and line together 
Python :: run python script automatically every day 
Python :: how to call a python script from another python script 
Python :: get column index pandas 
Python :: Neuraal Netwerk python text 
Python :: colorgram in python 
Python :: inverse mask python 
Python :: nested for loop table python 
Python :: remove vowels in a string python 
Python :: map to list python 
Python :: cite pandas python 
Python :: append string python 
Python :: change python version in colab 
Python :: python append to list 
Python :: get drive path python 
Python :: Box Plot, Python 
Python :: pandas dataframe drop rows with -ve in column value 
Python :: push in python 
Python :: permission denied when converting dataframe to csv 
Python :: leetcode matrix diagonal sum in python 
Python :: boxplot python 
Python :: Python: Extracting XML to DataFrame (Pandas) 
Python :: math module sin() function in python 
Python :: float64 python 
Python :: interface, abstract python? 
Python :: how to change datetime format to mmyy in dataframe 
Python :: pandas create average per group 
Python :: python tuple get index of element 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =