How To Shuffle A List With Gaussian Distribution
I want to simulate fault on a message (Eg: 1000010011 => 1010000011). Is there a way to implement this in Python? I tried the following, which works: import random a = '10111011
Solution 1:
c = lambda: random.gauss(0.5,0.1)
Solution 2:
The second argument of random.shuffle
should be a callable, not a float. Try:
random.shuffle(b, lambda:random.gauss(0.5,0.1))
To cap in the interval from 0 to 1, you could use
random.shuffle(b, lambda: max(0.0, min(1.0, random.gauss(0.5,0.1))))
(Thanks @DSM)
If you're pedantic, the above capping actually includes 1.0, which would lead to an error in random.shuffle
. You should in fact replace 1.0 by the largest float smaller than 1.0.
Post a Comment for "How To Shuffle A List With Gaussian Distribution"