How can I randomly iterate through a large Range?
Posted
by void
on Stack Overflow
See other posts from Stack Overflow
or by void
Published on 2010-03-17T04:30:43Z
Indexed on
2010/03/19
4:21 UTC
Read the original article
Hit count: 360
I would like to randomly iterate through a range. Each value will be visited only once and all values will eventually be visited. For example:
class Array
def shuffle
ret = dup
j = length
i = 0
while j > 1
r = i + rand(j)
ret[i], ret[r] = ret[r], ret[i]
i += 1
j -= 1
end
ret
end
end
(0..9).to_a.shuffle.each{|x| f(x)}
where f(x)
is some function that operates on each value. A Fisher-Yates shuffle is used to efficiently provide random ordering.
My problem is that shuffle
needs to operate on an array, which is not cool because I am working with astronomically large numbers. Ruby will quickly consume a large amount of RAM trying to create a monstrous array. Imagine replacing (0..9)
with (0..99**99)
. This is also why the following code will not work:
tried = {} # store previous attempts
bigint = 99**99
bigint.times {
x = rand(bigint)
redo if tried[x]
tried[x] = true
f(x) # some function
}
This code is very naive and quickly runs out of memory as tried
obtains more entries.
What sort of algorithm can accomplish what I am trying to do?
[Edit1]: Why do I want to do this? I'm trying to exhaust the search space of a hash algorithm for a N-length input string looking for partial collisions. Each number I generate is equivalent to a unique input string, entropy and all. Basically, I'm "counting" using a custom alphabet.
[Edit2]: This means that f(x)
in the above examples is a method that generates a hash and compares it to a constant, target hash for partial collisions. I do not need to store the value of x
after I call f(x)
so memory should remain constant over time.
[Edit3/4/5/6]: Further clarification/fixes.
© Stack Overflow or respective owner