Different degree of shuffling using random python module

I use two architectural programs with visual programming plugins (Grasshopper for Rhino and Dynamo for Revit - for those who know / are interested)

Grasshopper contains a function called "Jitter" that will shuffle the list, however it has an input from 0.0 to 1.0 that controls the degree of shuffling - 0.0 causes no shuffling of 1.0 to produce a complete shuffle.

The second program (Dynamo) does not contain this function. It contains a shuffle module (which contains the initial value), but this is a complete random shuffle.

Ultimately, the goal is to create a series of solid and glazed panels, but produce a small random effect (but avoiding a large accumulation of solid and glazed elements - hence, I want an “easy shuffle”)

I wrote code that will calculate the number of required glazed (True) and solid (False) values, and then evenly distribute True and False values ​​depending on the number of specified elements and percent.

I checked the link to a random module, but I am not familiar with the various distributions as described.

Can someone help or point me in the right direction if an existing function achieves this.

(I changed a bit by adding True False one at a time to make up the correct number of elements in the list - list3 is the final list, list2 contains the repeated module of true folds)

Many thanks

import math
import random

percent = 30
items = 42

def remainder():
    remain = items % len(list2)

    list3.append(True)
    remain -= 1

    while remain > 0 :
        list3.append(False)
        remain -= 1

    return list3

#find module of repeating True and False values
list1 = ([True] + [False] * int((100/percent)-1))

#multiply this list to nearest multiple based on len(items)
list2 = list1 * int(items/(100/percent))

# make a copy of list2
list3 = list2[:]

#add alternating true and false to match len(list3) to len(items)
remainder()

#an example of a completely shuffled list - which is not desired 
shuffled = random.sample(list3, k = len(list3))
+4
source share
3 answers

Here is an approach based on this article that proves the result of the mixing time needed to scramble the list using adjacent element swaps

from random import choice
from math import log

def jitter(items,percent):
    n = len(items)
    m = (n**2 * log(n))
    items = items[:]
    indices = list(range(n-1))
    for i in range(int(percent*m)):
        j = choice(indices)
        items[j],items[j+1] = items[j+1],items[j]
    return items

Test, each line shows the result jitterwith different percentages applied to the same list:

ls = list(('0'*20 + '1'*20)*2)

for i in range(11):
    p = i/10.0
    print(''.join(jitter(ls,p)))

Typical Output:

00000000000000000000111111111111111111110000000000000000000011111111111111111111
00000000000000111100001101111011011111001010000100010001101000110110111111111111
00000000100100000101111110000110111101000001110001101001010101100011111111111110
00000001010010011011000100111010101100001111011100100000111010110111011001011111
00100001100000001101010000011010011011111011001100000111011011111011010101011101
00000000011101000110000110000010011001010110011111100100111101111011101100111110
00110000000001011001000010110011111101001111001001100101010011010111111011101100
01101100000100100110000011011000001101111111010100000100000110111011110011011111
01100010110100010100010100011000000001000101100011111011111011111011010100011111
10011100101000100010001100100000100111001111011011000100101101101010101101011111
10000000001000111101101011000011010010110011010101110011010100101101011110101110

I'm not sure how important this is, but it seems like a reasonable place to start.

+2
source

, " " (d), . : " (1-d)".

:

  • (1-d) * N

def partial_shuffle(x, d):
    """
    x: data to shuffle
    d: fraction of data to leave unshuffled
    """
    n = len(x)
    dn = int(d*n)
    indices = list(range(n))
    random.shuffle(indices)
    ind_fixed, ind_shuff = indices[dn:], indices[:dn]

    # copy across the fixed values
    result = x[:]

    # shuffle the shuffled values
    for src, dest in zip(ind_shuff, sorted(ind_shuff)):
        result[dest] = x[src]

    return result
+2

, , , Fisher-Yates .

O (n) shuffle , ..

, , [0,1], , .

, , "" .

Therefore, create a list of array indexes, shuffle them completely, and then use indexes as input to the Fisher-Yates algorithm to partially sort the original array.

+1
source

Source: https://habr.com/ru/post/1668457/


All Articles