Generating Random String Using PHP, JavaScript, Ruby, Python

Generate Random String Number in PHP JavaScript Ruby Python

Sometime in the programming world, you may need to generate a random string for any purpose, just like to random password suggestions, random passwords for your users.

Today, In this Article we will study how can we achieve that in the easiest and fastest way. Random password / Random String can be generated with fully-custom functions but when we compare that with custom function those uses pre-defined functions of that language then that will be the best and fastest. As pre-defined function’s definitions are already compiled where the fully-custom functions are not compiled. Compiler/Interpreter will compile the code (fully-custom function) before executing that. And that will take a couple of microseconds to make it executable. But when it comes with vast project microseconds execution may affect the project performance.


Generating Random String Using PHP

Using random_bytes() & bin2hex() functions

  • The random_bytes() function Generates cryptographically secure pseudo-random bytes.
  • The bin2hex() function Convert binary data into hexadecimal representation.

First we can use random_bytes() that will give us random bytes, which we can later convert that to hexadecimal format using bin2hex() function.

Random String Using Hashing Functions

PHP has a many hashing functions like sha1(), hash(), md5(), etc that can be used to hash a string/number based on certain algorithms i.e. “sha1”, “sha256”, “md5”, etc. You can use hash_algos() function to check a list of registered hashing algorithms. All these function takes a string/number as an argument and output an Alpha-Numeric hashed string of certain length.

Random String using sha1() Function

Random String using hash() Function

hash_algos() function to check a list of registered hashing algorithms. You can check a list of registered hashing algorithms and their hashed value length.

Random Strin using md5() Function

Random String Using Custom PHP Function

In some cases, if you do not want to use the above all techniques to get random string in PHP. Then you can use the below custom function to get random string.

Generate Random String Using JavaScript/jQuery

I JavaScript/jQuery generating a random string is quite easy through Math.random() function, later we can convert that random float number to string with the help to to_string function. But as the result will be floating number so we can extract substring from that with the help of substring() function.

The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1)], inclusive of 0, but not 1. It will return a floating number and that will be lesser than 1.

Generate Random String Using Ruby

Generate Random Capital Alphabetical String in Ruby

Random Capital Alphabetical String in Ruby – Example 1

Random Capital Alphabetical String in Ruby – Example 2

Generate Random Lower Alphabetical String in Ruby

Random Lower Alphabetical String in Ruby – Example 1

Random Lower Alphabetical String in Ruby – Example 2

Generate Random Capital & Lower Alphabetical String in Ruby

Random Capital & Lower Alphabetical String in Ruby – Example 1

Random Capital & Lower Alphabetical String in Ruby – Example 2

Generate Random Alpha-Numeric String in Ruby

Random Alpha-Numeric String in Ruby – Example 1

Random Alpha-Numeric String in Ruby – Example 2

Generate Random Number Using SecureRandom in Ruby

SecureRandom number generator interface. This library is an interface to secure random number generators which are suitable for generating session keys in HTTP cookies, etc. It has lots of pre-defined functions that we can use. It’s a fastest & secure way to generate random string.

Generate Random String Using Python

In Python to get random string we just need to import 2 pre-defined packages. Later we can just use their methods to generate random string.

Generate Random Capital & Lower Alphabetical String in Python

Generate Random Lower Alphabetical String in Python

Generate Random Capital Alphabetical String in Python

Generate Random Alpha-Numeric String in Python

Leave a Reply