9/25/2023 0 Comments Random permutation python![]() My expressions round each random number and then add a unique small number (which is smaller than the rounding increment) to do this. To circumvent this issue, I would use modified random numbers that are forced to be different from all of the others in the list. It is absolutely possible, but the techniques for setting this up are far from obvious.įor each new row, to generate numbers pointing to positions in the original row, I would rank a series of random numbers by size from largest to smallest, using the RANK() and RAND() functions.Įxcel does not prevent RAND() from generating the same random number more than once. J = CLng(((UBound(InArray, 2) - N) * Rnd) + N) ReDim Arr(1 To 1, LBound(InArray, 2) To UBound(InArray, 2))įor N = LBound(InArray, 2) To UBound(InArray, 2) L = UBound(InArray, 2) - LBound(InArray, 2) + 1 ' This function returns the values of InArray in random order. Pearsonįunction ShuffleArray(InArray() As Variant) As Variant() ShtOut.Range("a1").Resize(UBound(arrOut, 1), UBound(arrOut, 2)).Value = arrOut 'Populate output array with n randomly permuted sets ReDim arrOut(1 To pcount, 1 To UBound(arrIn, 2)) As Variant Pcount = Application.InputBox("How many permutations would you like?", Type:=1) 'Get number of permutations wanted from user input (Input must be in a single row.)", Type:=8) Set ValuesToPermute = Application.InputBox("Select values to permute. Sub nPerm()ĭim ValuesToPermute As Range, arrIn() As Variant, arrTmp() As Variantĭim arrOut() As Variant, shtOut As Worksheet The permutations will be printed to a new sheet, where you can copy and paste them wherever you like. The macro below will allow you to select the values you want to shuffle and specify the number of permutations you'd like to create. This is good for a one-off or a small number of rows.įor a more robust solution, I would use VBA. Then fill the formula below across A3:G3. I think this simplifies the formula and makes it a little easier to understand.įor sample data in A1:G1: dog mouse rhino ape cat fish rat > print(''.join(random.I use a method similar to what Gary's Student posted, but I use RANK in my formula instead. If you call random sample the following way, you get a repeatable random permutation function: > ed(53434) ![]() It looks like I did not use the seed properly (or at all). I believe that there is a canonical way to get these random permutation functions and I need someone to show that canonical way. So, a few minutes of searching does not produce anything. Sr is not the same, meaning that you don't "have" a FIXED, random permutation function. That means that when you call it again sr = ''.join(random.sample(s, len(s))) This is a random permutation, but it is not repeatable. Which leads to this code: sr = ''.join(random.sample(s, len(s))) To show you what I mean, I did a quick search and found this site: ![]() Or, in other words, I want a way to create new fix, random permutation functions by supplying a seed. 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Įssentially, 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. It would work like a function, f, where the following is true: Specifically, I want a fixed permutation of each element that is defined by a seed. 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |