Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random as rnd
- import time
- import os
- from matplotlib import pyplot as plt
- def lcg(seed, n, r):
- m = 2**31
- a = 1103515245
- c = 12345
- nums = []
- indicies = []
- x = seed
- for i in range(n):
- x = (a * x + c) % m
- nums.append(x % r)
- indicies.append(i)
- return nums, indicies
- def random(seed, n, r):
- nums = []
- indicies = []
- rnd.seed(seed)
- for i in range(n):
- x = rnd.randint(0, r)
- nums.append(x)
- indicies.append(i)
- return nums, indicies
- if __name__ == "__main__":
- print("Enter N (amount of numbers)")
- n = int(input())
- print("Enter R (maximum possible number)")
- r = int(input())
- seed = time.time()
- lcg_nums, lcg_indicies = lcg(seed, n, r)
- random_nums, random_indicies = random(seed, n, r)
- fig = plt.figure(figsize=(20, 10))
- lcg_plot = fig.add_subplot(1, 2, 1)
- lcg_plot.set_title("linear congruential generator")
- lcg_plot.scatter(lcg_indicies, lcg_nums)
- random_plot = fig.add_subplot(1, 2, 2)
- random_plot.set_title("built-in random")
- random_plot.scatter(random_indicies, random_nums)
- fig_path = os.path.join(os.path.dirname(__file__), 'rnd.png')
- fig.savefig(fig_path)
- print("Saved result as {}".format(fig_path))
Add Comment
Please, Sign In to add comment