Generating Random User ID

Hey there,

A while back I created some code to generate a random number and was wondering if anyone can provide a ore efficient method..

Here is what I Started with...

aStr = "" 11.times { aStr << (rand(9)+1).to_s } [3,7].each { |i| aStr = '-' } aStrHere is the revision I came up with after a little tinkering...

(t=(0..8).inject("") do |r,i| r << ((c="#{(rand(9)+1)}"); c<<"-" if i % 3 == 2; c) end)[0,t.size-1]And finally, here is the masterpiece I came up with (after much brain scratching)

def generate_id (t=(0..2).inject("") { |r,i| r << "#{rand(899)+100}-"})[0,t.size-1]endWhen you run 'generate_id' method, it creates a string of 9 numbers (delimited every 3 with '-').

Obviously, it could easily be modified to generate a longer id and can also easily be changed to an integer value. (by doing something like this)

id_value = generate_idid_value.gsub("-","").to_iAnyway..

Are you guys able to provide an even better method for generating a random id ?

(also - I understand this does not check for any id's already defined - thats already taken care of)
Code:
rand(10**8...10**9).to_s.scan(/.{3}/).join('-')
Edit: Oh, that doesn't generate 000-000-000 to 099-999-999. But this does:
Code:
sprintf("%09d", rand(10**9)).scan(/.{3}/).join('-')
What about SecureRandom?

Comes with ruby so you can just grab it from there.

Granted, it requires you to load an .so, but you can already do that.

Code:
begin  require 'openssl'rescue LoadErrorend
Code:
require 'openssl.so'require 'openssl/bn'require 'openssl/cipher'require 'openssl/config'require 'openssl/digest'require 'openssl/x509'require 'openssl/ssl'
oh, that is quite the line of code you have there cremno - very nice indeed.  I assume if I was wanting to make the value larger, say 4 chars before each separation I could change the 9 to a 12 and the 3 to a 4, like this?

sprintf("%09d", rand(10**12)).scan(/.{4}/).join('-')Looking into the SecureRandom now. Thanks for the link 🙂
I probably would use keyword arguments (okay, in RGSS / 1.9 maybe not) and check the arguments for valid values, but something along these lines should make that method more general:

Code:
def generate_id(ndigits = 9, chunksize = 3, separator = '-')  sprintf("%0*d", ndigits, rand(10**ndigits)).scan(/.{#{chunksize}}/).join(separator)end
Thats nice, and also much more readable than what I came up with. Thanks very much 😀
A bit late, but for the sake of completion some solutions I'd probably have used: 🙂

If you don't want zeros at all:

CHARACTER_SET = ('1'..'9').to_a
...
"XXX-XXX-XXX".gsub("X") { CHARACTER_SET.sample }


An alternative if you just don't want leading zeros:

Array.new(3) { (100 + rand(900)).to_s }.join("-")
I like that too. 😛

There is probably hundreds of ways such a thing could be implemented. What I have atm is this...

https://github.com/Dekita/Dekyde-Ace/blob/master/Serial%20Code%20Generator.rb

Edited code in spoiler 🙂

Original thread: https://forums.rpgmakerweb.com/threads/generating-random-user-id.35614/