So I wrote a new way to create a matrix without the use of numpy or np zeros.
import numpy as np
import time
x = [1,2,3,4,5,6,7,8,9,10]
x1 = 1
def fakenpzeros(size):
rows, cols = 1, size
matrix = [([0]*cols) for i in range(rows)]
return matrix
print(np.zeros(x1))
print(fakenpzeros(x1))
The console would output
[[0]] (for the function I created) and [0.] (for the numpy function)
I want to introduce my function into my code as I am trying to create a random sampling function without numpy or random
This is my following code:
def pseudo_uniform_bad(mult=5,
mod=11,
seed=1,
size=1):
"""
A bad pseudo random generator with small multipliers and modulus
"""
U = np.zeros(size)
#U = fakenpzeros(size)
x = (seed*mult+1)%mod
U[0] = x/mod
for i in range(1,size):
x = (x*mult+1)%mod
U[i] = x/mod
return U
def pseudo_uniform_good(mult=16807,
mod=(2**31)-1,
seed=123456789,
size=1):
"""
A reasoanbly good pseudo random generator
"""
U = np.zeros(size)
#U = fakenpzeros(size)
x = (seed*mult+1)%mod
U[0] = x/mod
for i in range(1,size):
x = (x*mult+1)%mod
U[i] = x/mod
return U
def pseudo_uniform(low=0,
high=1,
seed=123456789,
size=1):
"""
Generates uniformly random number between `low` and `high` limits
"""
return low+(high-low)*pseudo_uniform_good(seed=seed,size=size)
def sample_pick(lst):
"""
Picks up a random sample from a given list
"""
# Sets seed based on the decimal portion of the current system clock
t = time.perf_counter()
seed = int(10**9*float(str(t-int(t))[0:]))
# Random sample as an index
l = len(lst)
s = pseudo_uniform(low=0,high=l,seed=seed,size=1)
idx = int(s)
return (lst[idx]) #SAMPLING
print(sample_pick(x1))
However, when I replace np.zeros(size)
with fakenpzeros(size)
it says something along the lines of:
unsupported operand type(s) for +: 'int' and 'list'
So how exactly can I convert my matrix into an integer/tuple of integers like how numpy does it?