mercredi 15 mai 2019

Generating random lists in Python (seed problem?)

I'm trying to generate some (pseudo-) random lists for testing purposes. Here I'm generating a 2x2 matrix (list of lists), where 1 test_data.json file has any positive number of "cycles" & each "cycle" has n (resolution) number of integers.

After trying some basic random() functions from numpy & random libraries, I've been unable to generate lists randomly at all.

import json
import random as ran
import numpy as np
import os

resolution = 10 # Map resolution: Max = 200
cycles = 3 # Number of cycles
dist = [None for _ in range(resolution)] # Distance list
output = list()
n = 0

with open("test_data.json", "w") as test:
    for turn in range(cycles):
        n += 10
        # ran.seed(n)
        np.random.seed(n)
        for num in range(resolution):
            # dist[num] = int(ran.random() * 255)
            dist[num] = int(np.random.random() * 255)
        output.append(dist)
    # print(output)
    json.dump(output, test)
        # test.write('\n')

I can work with any "random" output within a certain range (here I'm scaling 0-1 to 0-255). Numbers in each list (cycle) is random enough, but every cycle is the same list of numbers. [[164, 97, 169, 41, 245, 88, 252, 59, 149, 103], [164, 97, 169, 41, 245, 88, 252, 59, 149, 103], [164, 97, 169, 41, 245, 88, 252, 59, 149, 103]] I've tried using seed(), with constant and changing seeds but the output is never changing between cycles.




Aucun commentaire:

Enregistrer un commentaire