<jupyter_start><jupyter_text>Pseudo RandomI will use this notebook to investigate the power-residue method to generate pseudo-random numbers.Let $a$,$c$ and $M$ be constants. Assume a seed $r_0$ is given. The sequence of numbers ${r_i}_{i=1,2,..}$ is generated as:$$ r_{i+1} \equiv a r_i + c \pmod{M}$$<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
def gen_r(r,a,c,M):
    return (a*r + c) % M
M = 512
r_0 = np.random.randint(M)
a = 7
c = 1

num_to_gen = 10000
rand_list = np.zeros(num_to_gen)
rand_list[0] = r_0
for i in range(num_to_gen - 1):
    rand_list[i + 1] = gen_r(rand_list[i],a,c,M)
plt.plot(rand_list,'o')
plt.title("Random Number Sequence")
# see the distribution
plt.hist(rand_list,bins="auto",density=True)
plt.title("Random number distirbution")
# comparison
computer_random = M*np.random.rand(num_to_gen)
score = np.mean(computer_random - np.array(rand_list))
print("Score",score)
# study of score vs num_to_gen
def gen_random_list(num_to_gen,a,c,M):
    r_0 = np.random.randint(M)
    
    rand_list = np.zeros(num_to_gen)
    rand_list[0] = r_0
    for i in range(num_to_gen - 1):
        rand_list[i + 1] = gen_r(rand_list[i],a,c,M)
    
    return np.array(rand_list)

def calc_score(rand_list,M):
    '''
        Score is the mean of the difference between computer generated random sequence and my sequence
    '''
    computer_random = M*np.random.rand(len(rand_list))
    score = np.mean(computer_random - rand_list)
    dev = np.std(computer_random - rand_list)
    return score,dev

num_gen_list = np.logspace(2,6,100)

score_list = []
dev_list = []
for num in num_gen_list:
    rand_list = gen_random_list(int(num),a,c,M)
    score,dev = calc_score(rand_list,M)
    score_list.append(score)
    dev_list.append(dev)
plt.semilogx(num_gen_list,score_list)
plt.xlabel("Number of random numbers generated")
plt.ylabel("Score wrt. a computer generate sequence")
plt.semilogx(num_gen_list,dev_list)
plt.xlabel("Number of random numbers generated")
plt.ylabel("Std. dev. wrt. a computer generate sequence")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1279847182425979, 'fraction_numerical': 0.013849092645654251, 'mean_word_length': 4.692934782608695, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '427624', 'license': 'MIT', 'n_tokens_mistral': 830, 'n_tokens_neox': 793, 'n_words': 184, 'path': 'Pseudo Random.ipynb', 'repo_name': 'sandeshkalantre/random-numerical-experiments', 'size': '2094'}