Improve RNG
This commit is contained in:
parent
ebeaf12bfb
commit
a86b6052f2
@ -1,7 +1,9 @@
|
|||||||
import math
|
import math
|
||||||
import random
|
|
||||||
import itertools
|
|
||||||
import pyaudio
|
import pyaudio
|
||||||
|
from struct import unpack
|
||||||
|
from random import shuffle, sample
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
opzioni = {
|
opzioni = {
|
||||||
"format": pyaudio.paFloat32,
|
"format": pyaudio.paFloat32,
|
||||||
@ -65,26 +67,35 @@ class primes:
|
|||||||
|
|
||||||
class generator:
|
class generator:
|
||||||
|
|
||||||
def random(start, stop, n):
|
def _nums(l, n):
|
||||||
"""
|
"""
|
||||||
Returns n random integers in the range [start, end).
|
Split bytes l into n 32-bit unsigned int.
|
||||||
|
[Generator]
|
||||||
|
"""
|
||||||
|
for i in range(0, len(l), n):
|
||||||
|
chunk = l[i:i+n]
|
||||||
|
if any(chunk): # throw away initial zeros
|
||||||
|
yield unpack('>L', chunk)[0]
|
||||||
|
|
||||||
|
def random(a, b, n):
|
||||||
|
"""
|
||||||
|
Returns n random 32-bit float in the range [a, b).
|
||||||
[Generator]
|
[Generator]
|
||||||
"""
|
"""
|
||||||
audio = pyaudio.PyAudio()
|
audio = pyaudio.PyAudio()
|
||||||
input = audio.open(**opzioni)
|
raw_data = audio.open(**opzioni).read(
|
||||||
dati = input.read(
|
|
||||||
opzioni["rate"] // opzioni["frames_per_buffer"] *
|
opzioni["rate"] // opzioni["frames_per_buffer"] *
|
||||||
int(math.log(n)) + 500)
|
int(10*math.log(n)))
|
||||||
input.close()
|
|
||||||
audio.terminate()
|
audio.terminate()
|
||||||
dati = [i for i in dati if i != 0]
|
|
||||||
dati = [i for i in dati if i in range(start, stop)]
|
sequence = generator._nums(raw_data, 4)
|
||||||
random.shuffle(dati)
|
sequence = sample(list(sequence), n)
|
||||||
for i in range(n):
|
m = max(sequence)
|
||||||
yield dati[i]
|
|
||||||
|
return map(lambda x: a + (b-a) * (x/m), sequence)
|
||||||
|
|
||||||
def onetimepad(l):
|
def onetimepad(l):
|
||||||
"""
|
"""
|
||||||
Return an l-uple of random integers for a one-time-pad.
|
Return an l-uple of random integers for a one-time-pad.
|
||||||
"""
|
"""
|
||||||
return tuple(generator.random(0, 2 * 16, l))
|
return tuple(map(int, generator.random(0, 2*16, l)))
|
||||||
|
Loading…
Reference in New Issue
Block a user