0

I'm trying to generate some (pseudo-) random lists for testing purposes. Here I'm generating a 2x2 matrix (list of lists), where one 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.

1
  • ctr+f seed and remove every instance of it. Then you'll have random. Commented May 16, 2019 at 6:08

3 Answers 3

3

Don't set the seed at every iteration. The whole point of a seed is that a given seed will generate the same stream of numbers every single time. Set the seed once at the beginning of your program (to get the same results for each run), or not at all. In the latter case, a time-dependent initial state will be generated for you, making your generator appear random indeed.

Also, choose whether you want to use python's built-in random module or np.random. You probably don't want to use both. Especially not if you're setting seeds. The seeds of one don't affect the other.

Sign up to request clarification or add additional context in comments.

2 Comments

I've tried not using seed() at all and that gave me repeated lists like the ones I'm currently getting. Also, I'm not using both random & np. They're just both there for testing.
Oh yeah, in your attempt to pre-allocate dist, you ended up appending the same reference to output over and over. The accepted answer solves this without telling you what the problem was. Your numbers aren't coming out the same, you're only seeing the last batch. If you allocate dist inside the outer loop, you'll see the difference.
2

Why not remove the seeds, and simply use

import random as ran

ran.randint(0, 256)

to generate your random numbers in the range 0-255?

Comments

0

you can solve this by simply using random only

import random

cycles = 3
resolution = 10
output = [[random.randint(0,256) for _ in range(resolution)] for _ in range(cycles)]

This will give output as follows:

[[130, 126, 153, 18, 58, 24, 184, 75, 14, 25], [215, 73, 2, 58, 170, 255, 34, 113, 83, 80], [82, 100, 0, 118, 181, 90, 113, 165, 57, 87]]

You can then dump output to file

open("test_data.json", "w") as test:
      json.dump(output, test)

The no of cycles and resolutions can be changed to achieve desirable results

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.