<jupyter_start><jupyter_text>Excercise to create random number generator with predefined distribution functionnp.random.random returns values in the half-open interval [0, 1) which means equal to or grater than zero, and up to but less than 1<jupyter_code>import random
import numpy as np
import matplotlib.pyplot as plt
from functools import reduce
from rc_random import RandomGen
%matplotlib inline
import math

np.random.seed(1000)
?math.fsum
?np.random.random()
random_nums = [-1, 0, 1, 2, 3]
probabilities = [0.01, 0.3, 0.58, 0.1, 0.01]<jupyter_output><empty_output><jupyter_text>Find baskets that randomly drawn values fall into, then assign the predetermined number covering that basket<jupyter_code>cumsum = [sum(probabilities[:i+1]) for i, v in enumerate(probabilities)]<jupyter_output><empty_output><jupyter_text>cumsum = [math.fsum(probabilities[:i+1]) for i, v in enumerate(probabilities)]   Its ok we didnt used this as numpy never returns 1.0<jupyter_code>cumsum
rand = np.random.random()
rand
index_greater_than_rand = [i for i, v in enumerate(cumsum) if v > rand]  # return cumsum indexes greater than the rand
print([cumsum[i] for i in index_greater_than_rand])
index_greater_than_rand[0]
random_nums[index_greater_than_rand[0]]<jupyter_output><empty_output><jupyter_text>TestingMore pythonic to create a generator?<jupyter_code>g1 = RandomGen(random_nums, probabilities)
g1.next_num()
%%time
test = [g1.next_num() for i in range(1000000)]
plt.hist(test)<jupyter_output><empty_output><jupyter_text>Lets get the number of counts<jupyter_code>unique, counts = np.unique(test, return_counts=True)
dict(zip(unique, counts))
counts<jupyter_output><empty_output><jupyter_text>Now we compare the relative counts with the relative probabilities<jupyter_code>norm_counts = counts/max(counts)
norm_counts
norm_probs = np.array(probabilities)/max(probabilities)
norm_probs
diff = norm_counts/norm_probs
diff
diff_spread_percent = 100.0*(max(diff) - min(diff))
max_diff = int(diff_spread_percent)
print("So for {} samples the error is {} percent".format(test_len, max_diff))
x = 3.14159
x.as_integer_ratio()
3537115888337719/1125899906842624
y = 0.1
y.as_integer_ratio()
3602879701896397/36028797018963968<jupyter_output><empty_output><jupyter_text>Normal dist<jupyter_code>x = np.linspace(-10,10,1000)
mu = -1
sigma = 1
y = ((2*np.pi*sigma)**-0.5) * np.exp(-((x-mu)**2)/(2*sigma**2))
plt.plot(x,y)<jupyter_output><empty_output><jupyter_text>Pseudo random generator<jupyter_code>def lcg(modulus, a, c, seed):
    while True:
        seed = (a * seed + c) % modulus
        yield seed
modulus = 3 
a = 2 
c = 3 
seed = 2304283
my_gen = lcg(modulus, a, c, seed)
next(my_gen)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11451791651274473, 'fraction_numerical': 0.05319541928333949, 'mean_word_length': 6.804034582132565, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '193185', 'license': 'MIT', 'n_tokens_mistral': 1094, 'n_tokens_neox': 984, 'n_words': 238, 'path': 'Random/random choice/Random generator 28-12-2017.ipynb', 'repo_name': 'afcarl/Useful-python', 'size': '2707'}