Generating a Random Code in PHP

generating a random code in php?

I would rather use md5 to generate passwords

But you can use something like this if you want a custom:

function createRandomPassword() { 

$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;

while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}

return $pass;

}

PHP random string generator

To answer this question specifically, two problems:

  1. $randstring is not in scope when you echo it.
  2. The characters are not getting concatenated together in the loop.

Here's a code snippet with the corrections:

function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}

Output the random string with the call below:

// Echo the random string.
// Optionally, you can give it a desired string length.
echo generateRandomString();

Please note that this generates predictable random strings. If you want to create secure tokens, see this answer.

Generate random code with PHP

This should be what you're looking for. If you want uppercase letters, just add them to the $chars string.

<?php
$chars = "abcdefghijklmnopqrstuvwxyz";
function generateCode( $chars , $limit = 5 ){
$charsLength = strlen( $chars );
$code = $chars[rand(1,$charsLength)-1];
$chars .= "0123456789";
$charsLength = strlen( $chars );
while(strlen($code) < $limit){
$code .= $chars[rand(1,$charsLength)-1];
}
}

$myCode = generateCode( $chars );

generate 14 digit random number in php

I won't get into a discussion about how "random" we can ever really get, I am pretty sure this will generate a number random enough! :)

function randomNumber($length) {
$result = '';
for($i = 0; $i < $length; $i++) {
$result .= mt_rand(0, 9);
}
return $result;
}

..and of course then it's just echo randomNumber(14);

Generate an N-digit random number

Something like this ?

<?php 
$a = mt_rand(100000,999999);
?>

Or this, then the first digit can be 0 in first example can it only be 1 to 9

for ($i = 0; $i<6; $i++) 
{
$a .= mt_rand(0,9);
}

How to generate a random, unique, alphanumeric string?

Security Notice: This solution should not be used in situations where the quality of your randomness can affect the security of an application. In particular, rand() and uniqid() are not cryptographically secure random number generators. See Scott's answer for a secure alternative.

If you do not need it to be absolutely unique over time:

md5(uniqid(rand(), true))

Otherwise (given you have already determined a unique login for your user):

md5(uniqid($your_user_login, true))

How can I generate a 6 digit unique number?

$six_digit_random_number = random_int(100000, 999999);

As all numbers between 100,000 and 999,999 are six digits, of course.

Generating UNIQUE Random Numbers within a range

Array with range of numbers at random order:

$numbers = range(1, 20);
shuffle($numbers);

Wrapped function:

function UniqueRandomNumbersWithinRange($min, $max, $quantity) {
$numbers = range($min, $max);
shuffle($numbers);
return array_slice($numbers, 0, $quantity);
}

Example:

<?php
print_r( UniqueRandomNumbersWithinRange(0,25,5) );
?>

Result:

 Array
(
[0] => 14
[1] => 16
[2] => 17
[3] => 20
[4] => 1
)

php Random string generation with letters and numbers

Use the below code.

For digits and characters

$length = 10;    
echo substr(str_shuffle('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'),1,$length);

For only characters

$length = 10;    
echo substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ'),1,$length);

For only digits

$length = 10;    
echo substr(str_shuffle('0123456789'),1,$length);


Related Topics



Leave a reply



Submit