Last active
August 29, 2015 13:56
-
-
Save JoshCheek/8907567 to your computer and use it in GitHub Desktop.
Keep getting the same value for next_seed as I get for seed.
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
class RandomCollectionQueue | |
def initialize(seed, collection) | |
random = ::Random.new seed | |
self.collection = collection | |
self.seed = seed | |
self.next_seed = random.rand | |
self.block_index = random.rand collection.size | |
p [seed, next_seed] | |
end | |
def dequeue | |
@dequeued ||= [ | |
self.class.new(next_seed, collection), | |
collection[block_index] | |
] | |
end | |
private | |
attr_accessor :seed, :next_seed, :block_index, :collection | |
end | |
q = RandomCollectionQueue.new rand, [*1..10] | |
q, n = q.dequeue # => [#<RandomCollectionQueue:0x007fafe8b49a70 @collection=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], @seed=0.5488135039273248, @next_seed=0.5488135039273248, @block_index=5>, 6] | |
q, n = q.dequeue # => [#<RandomCollectionQueue:0x007fafe8b53980 @collection=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], @seed=0.5488135039273248, @next_seed=0.5488135039273248, @block_index=5>, 6] | |
q, n = q.dequeue # => [#<RandomCollectionQueue:0x007fafe8b52238 @collection=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], @seed=0.5488135039273248, @next_seed=0.5488135039273248, @block_index=5>, 6] | |
# >> [0.5799812254037409, 0.5488135039273248] | |
# >> [0.5488135039273248, 0.5488135039273248] | |
# >> [0.5488135039273248, 0.5488135039273248] | |
# >> [0.5488135039273248, 0.5488135039273248] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Figured it out. It's a fucking type error.
Kernel.rand
returns a float between 0 and 1.Random#initialize
must callFloat#to_i
on it, meaning it seeds with 0 every time.