lundi 17 mai 2021

A fixed, random permutation with a seed [closed]

I want to take a column, C1, of strings in a dataframe, or you could just imagine a list of strings, and I want to get a new column where the entries, by row, are random permutations of the elements in the rows in C1. Specifically, I want a fixed permutation of each element that is defined by a seed. It would work like a function, f, where the following is true:

f(a) = b

and if I run it again

f(a) = b

where b is a random permutation of a. I want f to be parameterized by some seed, so that it is easy for me to create a new function, g, that is also a random permutation satisfying

g(a) = c not equal to b

and when I run it again

g(a) = c not equal to b

Essentially, I want a list of random permutation functions, each of which is parameterized by something (say an integer) and is repeatable, given the fixed seed. Or, in other words, I want a way to create new fix, random permutation functions by supplying a seed.

To show you what I mean, I did a quick search and found this site:

https://note.nkmk.me/en/python-random-shuffle/

Which leads to this code:

sr = ''.join(random.sample(s, len(s)))

This is a random permutation, but it is not repeatable. That means that when you call it again

sr = ''.join(random.sample(s, len(s)))

sr is not the same, meaning that you don't "have" a FIXED, random permutation function. So, a few minutes of searching does not produce anything. I believe that there is a canonical way to get these random permutation functions and I need someone to show that canonical way.

Edit: It looks like I did not use the seed properly (or at all). If you call random sample the following way, you get a repeatable random permutation function:

>>> random.seed(53434)
>>> print(''.join(random.sample(a,len(a))))



Aucun commentaire:

Enregistrer un commentaire