Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 560 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to generate a random string in Ruby

#31
Another trick that works with Ruby 1.8+ and is fast is:

>> require "openssl"
>> OpenSSL::Random.random_bytes(20).unpack('H*').join
=> "2f3ff53dd712ba2303a573d9f9a8c1dbc1942d28"

It get's you random hex string. Similar way you should be able to generate base64 string ('M*').
Reply

#32
If you want a string of specified length, use:


require 'securerandom'
randomstring = SecureRandom.hex(n)

It will generate a random string of length `2n` containing `0-9` and `a-f`
Reply

#33
Why not use SecureRandom?

require 'securerandom'
random_string = SecureRandom.hex

# outputs: 5b5cd0da3121fc53b4bc84d0c8af2e81 (i.e. 32 chars of 0..9, a..f)

SecureRandom also has methods for:

- base64
- random_bytes
- random_number

see: [

[To see links please register here]

][1]


[1]:

[To see links please register here]

Reply

#34
This solution needs external dependency, but seems prettier than another.

1. Install gem [faker][1]
2. `Faker::Lorem.characters(10) # => "ang9cbhoa8"`


[1]:

[To see links please register here]

Reply

#35
10.times do
alphabet = ('a'..'z').to_a
string += alpha[rand(alpha.length)]
end

Reply

#36
Ruby 1.9+:

ALPHABET = ('a'..'z').to_a
#=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

10.times.map { ALPHABET.sample }.join
#=> "stkbssowre"

# or

10.times.inject('') { |s| s + ALPHABET.sample }
#=> "fdgvacnxhc"
Reply

#37
a='';8.times{a<<[*'a'..'z'].sample};p a

or

8.times.collect{[*'a'..'z'].sample}.join
Reply

#38
Given:

chars = [*('a'..'z'),*('0'..'9')].flatten

Single expression, can be passed as an argument, allows duplicate characters:

Array.new(len) { chars.sample }.join
Reply

#39
Here is a improve of @Travis R answer:

def random_string(length=5)
chars = 'abdefghjkmnpqrstuvwxyzABDEFGHJKLMNPQRSTUVWXYZ'
numbers = '0123456789'
random_s = ''
(length/2).times { random_s << numbers[rand(numbers.size)] }
(length - random_s.length).times { random_s << chars[rand(chars.size)] }
random_s.split('').shuffle.join
end

At @Travis R answer chars and numbers were together, so sometimes `random_string` could return only numbers or only characters. With this improve at least half of `random_string` will be characters and the rest are numbers. Just in case if you need a random string with numbers and characters
Reply

#40
Use 'SafeRandom' Gem [GithubLink][1]

It will provide the easiest way to generate random values for Rails2, Rails 3, Rails 4, Rails 5 compatible.

[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through