Skip to content

Commit

Permalink
examples: Add shuffle examples to install list
Browse files Browse the repository at this point in the history
goodshuffle.py - even shuffle
badshuffle.py - biased shuffle

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed Mar 25, 2019
1 parent 200fbdc commit 103f81c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 3 additions & 1 deletion examples/Makefile
Expand Up @@ -26,7 +26,9 @@ EXAMPLES = \
morse.py \
snek-bsd.py \
snek.py \
track-light.py
track-light.py \
badshuffle.py \
goodshuffle.py

install:
install -d "$(DESTDIR)$(EXAMPLEDIR)"
Expand Down
4 changes: 3 additions & 1 deletion examples/badshuffle.py
Expand Up @@ -2,13 +2,15 @@

import random

random.seed(time.monotonic())

# "Shuffle" a list, but *wrong*.

narray = 16
a = [0] * narray
h = [0] * narray

for _ in range(10000):
for _ in range(100000):
for i in range(narray):
a[i] = i
for i in range(narray):
Expand Down
26 changes: 26 additions & 0 deletions examples/goodshuffle.py
@@ -0,0 +1,26 @@
#!/usr/bin/python3

import random

random.seed(time.monotonic())

# "Shuffle" a list, but *wrong*.

narray = 16
a = [0] * narray
h = [0] * narray

for _ in range(100000):
for i in range(narray):
a[i] = i
for i in range(narray):
j = i + random.randrange(narray - i)
tmp = a[j]
a[j] = a[i]
a[i] = tmp
for i in range(narray):
if a[i] == narray - 1:
h[i] += 1
break

print(h)

0 comments on commit 103f81c

Please sign in to comment.