The Crystal Programming Language Forum

Using Random::ISAAC to generate random ids

I want to generate some random id values. I know there are many ways one can do this. But I want to understand why the code below is not working. It gives the same id most of the times; about 1/10 is different, but 9/10 it is the same. Where is the error, and how can I fix it? Thank you for the help.

require "random/isaac"

def rnd_alpha_numeric_id()
    srcstr = "abcdefghjkmnpqrstuvwxyz" ## all except i, l and o
    rndinst = Random::ISAAC.new
    rndstr = srcstr.chars
                   .sample(6, rndinst)
                   .join
                
    rndnum = rndinst.rand(100000..999999)
    return "#{rndstr}-#{rndnum}"
end
            
10.times do |i|
    puts "#{i}: #{rnd_alpha_numeric_id()}"
end

Link to Crystal playground: https://play.crystal-lang.org/#/r/7980

Yes, it’s a bug. Could you report it? Thank you!

Thank you @asterite. I have created the issue at https://github.com/crystal-lang/crystal/issues/7976

(Edit reason: Added link to the created issue).