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

"""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 number list

"""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 :: python 3.7 download 
Python :: how to format a file in python 
Python :: typer python 
Python :: twitter scraping python 
Python :: How to build a Least Recently Used (LRU) cache, in Python? 
Python :: python string [::-1] 
Python :: numpy random entries not repeat 
Python :: count number of subdirectories 
Python :: how to get left click input in pygame 
Python :: class variable in python 
Python :: python odd or even 
Python :: how to maximize the screen in selenium 
Python :: 151 - Power Crisis solution in python 
Python :: speak by a discord bot in python 
Python :: stdin and stdout python 
Python :: Python colon equals 
Python :: python for loop float increment 
Python :: python sound 
Python :: python windows api 
Python :: clear terminal in python 
Python :: connect to vvenv python 
Python :: python append 
Python :: Passing Arrays to Methods 
Python :: .corr() python 
Python :: install tabula 
Python :: index.py:14: RuntimeWarning: invalid value encountered in true_divide return np.dot(user, user2) / (norm(user) * norm(user2)) 
Python :: python check if string contains number 
Python :: __slots__ python example 
Python :: django-tool-bar 
Python :: python - How to subtract values from dictionaries 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =