Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Unique Random String With Letters And Numbers In Lower Case

How can I fix this code so it generates unique random letters and numbers in lower case?

api_string = (0...32).map{65.+(rand(25)).chr}.join     

At the moment, it generates only letters.

like image 391
donald Avatar asked May 11 '11 15:05

donald


People also ask

How do you generate unique random strings?

There are many ways to generate a random, unique, alphanumeric string in PHP which are given below: Using str_shuffle() Function: The str_shuffle() function is an inbuilt function in PHP and is used to randomly shuffle all the characters of a string passed to the function as a parameter.


2 Answers

If you are using ruby 1.9.2 you can use SecureRandom:

irb(main):001:0> require 'securerandom' => true irb(main):002:0> SecureRandom.hex(13) => "5bbf194bcf8740ae8c9ce49e97" irb(main):003:0> SecureRandom.hex(15) => "d2413503a9618bacfdb1745eafdb0f" irb(main):004:0> SecureRandom.hex(32) => "432e0a359bbf3669e6da610d57ea5d0cd9e2fceb93e7f7989305d89e31073690" 
like image 175
Vasiliy Ermolovich Avatar answered Sep 30 '22 02:09

Vasiliy Ermolovich


All letters and digits, that's how numbers are represented in base 36.

api_string = Array.new(32){rand(36).to_s(36)}.join 
like image 41
steenslag Avatar answered Sep 30 '22 02:09

steenslag