-1

I am reusing a code written by someone else. It has the following line included:

 df = (random.choice(x) for i in repeat(0))

However, because there is no random.seed, the output is different every time I rerun it, which makes sense.

I tried to fix this with random.Random(500).choice(x), but since it's a for loop, it will then give me the same value for every iteration, which is not what I want.

Does anyone know how I make this pseudo-random? Thanks!

Leo
  • 135
  • 10

2 Answers2

0

If the variable x doesn't get reassigned:

df = map(random.Random(500).choice, repeat(x))

If it does:

df = (choice(x)
      for choice in [random.Random(500).choice]
      for _ in repeat(0))

Try it online! (see the "Output" section)

no comment
  • 6,381
  • 4
  • 12
  • 30
-1
import random

nums = [1, 2, 3, 4, 5]

def repeat_random(choices):
    i = 0
    chosen = random.choice(choices)
    while i < 5:
        print(chosen)
        i += 1

repeat_random(nums)

# This will print a random number from the nums list 5 times

Ghost Ops
  • 1,710
  • 2
  • 13
  • 23
5idneyD
  • 204
  • 1
  • 8