Created
December 9, 2023 17:51
-
-
Save pncnmnp/8afb7903f61ec69a157287435a6347aa to your computer and use it in GitHub Desktop.
Test out bazzargh's variation of "Shuffling using Fibonacci hashing"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import math | |
import copy | |
import numpy as np | |
def shuffle_songs(songs): | |
"""Return a list of shuffled songs.""" | |
num_songs = len(songs) | |
golden_ratio = 0.618033988749895 | |
seed = random.random() | |
pos = 1 | |
while True: | |
curr_pos = ((pos * golden_ratio) + seed) % 1 | |
pos += 1 | |
to_pick = math.floor(curr_pos * num_songs) | |
artist, song = songs[to_pick] | |
songs.remove((artist, song)) | |
num_songs -= 1 | |
if not songs: | |
break | |
yield (artist, song) | |
def clustering_index(shuffled_songs): | |
clusters = sum( | |
1 | |
for i in range(len(shuffled_songs) - 1) | |
if shuffled_songs[i][1] == shuffled_songs[i + 1][1] | |
) | |
return clusters / (len(shuffled_songs) - 1) | |
def get_fuzzy_set(): | |
max_songs = 10 | |
max_artists = 10 | |
fuzzy_set = [] | |
num_artists = random.randint(4, max_artists) | |
for i in range(num_artists): | |
num_songs = random.randint(1, max_songs) | |
for j in range(num_songs): | |
fuzzy_set.append(("artist{}".format(i), "song{}".format(j))) | |
return fuzzy_set | |
if __name__ == "__main__": | |
result = [] | |
N = 1000000 | |
for i in range(N): | |
songs = get_fuzzy_set() | |
shuffle = list(shuffle_songs(songs)) | |
measure = clustering_index(shuffle) | |
# if measure > 0.5 and measure < 0.6: | |
# print(shuffle) | |
result.append(measure) | |
np_arr = np.array(result) | |
print("P25:", np.percentile(np_arr, 25)) | |
print("P50:", np.percentile(np_arr, 50)) | |
print("P75:", np.percentile(np_arr, 75)) | |
print("P90:", np.percentile(np_arr, 90)) | |
print("P95:", np.percentile(np_arr, 95)) | |
print("Mean: ", sum(result) / N) | |
print("Median: ", sorted(result)[N // 2]) | |
print("Mode: ", max(set(result), key=result.count)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Discussion on https://news.ycombinator.com/item?id=38581959