I actually got this script from here... but it is not quite what I needed...
<?php
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
// Echo the random string.
// Optionally, you can give it a desired string length.
<?php echo generateRandomString(); ?>
?>
basically, I wanted this small php script to generate thousands of lines (~3000)...
in this format:
mytext/(the_random_string_here)
mytext/(the_random_string_here)
mytext/(the_random_string_here)
mytext/(the_random_string_here)
mytext/(the_random_string_here)
up to the very end (~3000 lines) with each line have a random string...
you can use a for loop to generate it 3000 times
for($i = 0; $i < 3000; $i++){
echo 'mytext/'.generateRandomString().'<br>';
}
The answer was inside your code snippet that you provided
If the number of lines is known, a for loop is a good choice. There is a nice example of such a loop right there in the generateRandomString function.
You could do something like this:
for ($i = 0; $i < 3000; $i++) {
echo "mytext/(" . generateRandomString() . ")<br>\n";
}
Related
I need to create a random number generator in php using mt_rand which should only generate random numbers. I know I need to use an infinite loop. How can I achieve this?
This is what I've already tried:
$query = "SELECT * FROM random";
$result = mysqli_query($link,$query);
$rows = mysqli_num_rows($result);
for($j = 0; $j < $rows; ++$j) {
$row=mysqli_fetch_assoc($result);
if($row["randomcol"]==$a) {
do {
echo "\n fetched";
echo $row["randomcol"];
echo "\n generated";
echo $a;
echo "\n repetition";
$a=mt_rand(1,10);
echo "\n Just after regeneration";
echo $a;
} while ($row["randomcol"]==$a);
}
}
Use rand() function.
$random_number = rand(1000,99999); //random number between 1000 and 99999
I have created sample random generator function in php. You can change length and characters according to your need.
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
I have a script which generates 6 character One Time Password (OTP).
Here is the code:-
$seed = str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.'0123456789'); // and any other characters
shuffle($seed); // probably optional since array_is randomized; this may be redundant
$rand = '';
foreach (array_rand($seed, 6) as $k)
$rand .= $seed[$k];
$feedID = $rand;
Now, due to shuffling procedure, currently all 6 can be digits, all 6 can be alphabets.
I want min and max 2 mandatory digits.
How can I do that?
Here's my take on it:
// Create a string of all alpha characters and randomly shuffle them
$alpha = str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
// Create a string of all numeric characters and randomly shuffle them
$numeric = str_shuffle('0123456789');
// Grab the 4 first alpha characters + the 2 first numeric characters
$code = substr($alpha, 0, 4) . substr($numeric, 0, 2);
// Shuffle the code to get the alpha and numeric in random positions
$code = str_shuffle($code);
If you want the possibility to have any character more than once, change the two first lines (quick and dirty):
// Let's repeat this string 4 times before shuffle, since we need 4 characters
$alpha = str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 4));
// Let's repeat this string 2 times before shuffle, since we need 2 numeric characters
$numeric = str_shuffle(str_repeat('0123456789', 2));
Not saying that this is the best way of doing it, but it's simple, without loops and/or arrays. :)
One more option.
Not saying that this is the best way of doing it, but it's simple, with loops and arrays. ;)
foreach ([4 => range('A', 'Z'), 2 => range(0, 9)] as $n => $chars) {
for ($i=0; $i < $n; $i++) {
$otp[] = $chars[array_rand($chars)];
}
}
shuffle($otp);
$otp = implode('', $otp);
$seed = str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
$seed2= str_split('0123456789');
$rand = [];
for($i=mt_rand(1,2);$i<=2;$i++){
shuffle($seed2);
$rand[]=$seed2[0];
}
while(count($rand)!=6){
shuffle($seed);
$rand[]=$seed[0];
}
shuffle($rand);
print $feedID = implode('',$rand);
you can use random() also to genarate the string with num + letters.LINK
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
Hope this help you
function generateRandomString($length = 10,$char_len=4,$numbre_len=2) {
$characters = '0123456789';
$characters2='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$charactersLength2 = strlen($characters);
$randomString = '';
for ($i = 0; $i <$char_len ; $i++) {
$randomString .= $characters2[rand(0, $charactersLength2 - 1)];
}
for ($i = 0; $i <$numbre_len ; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
$shuffled = str_shuffle($randomString);
return $shuffled;
}
$length=7;
$char_len=6;
$numbre_len=1;
echo generateRandomString($length,$char_len,$numbre_len);
This function may help to generate dynamic random otp which you want.
<?php
// Set length of the string
$length = 30;
$random = substr(md5(rand()), 0, 15);
// Use for-loop to generate thirty unique alphanumeric strings
For($i=0; $i<$length;) {echo $random.$i++."<br />";
The code above generates something like 5523d651bfb642b0, 5523d651bfb642b1, 5523d651bfb642b2, 5523d651bfb642b3 etc. The next value is just too easily predictable. I want something totally different like when I refresh the page in the browser. If I remove loop and just echo $random, the page generates a totally different string every time I refresh the page. How do I generate something totally different like 5523d651bfb642b0, 2yyd00nngbh201km, 78gdfmpqg01597v etc using loop? Thanks.
function generateRandomString($length = 30) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
$i = 0;
$times_to_run = 30;
while ($i++ < $times_to_run)
{
generateRandomString();
}
If you're using PHP 7, it's easy to generate a random string:
$bytes = random_bytes(5); //random. Increase input for more bytes
$code = bin2hex($bytes); // eg: 385e33f741
Before PHP 7, I would generate an alphanumeric code with a function
function getRandomString($length){
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
$result = '';
while(strlen($result)<$length) {
$result .= $chars{mt_rand(0,strlen($chars)-1)};
}
return $result;
}
To get a 10-character random code, you just call:
$randomString = getRandomString(10);
You can add more characters to $chars ofcourse
How to get a fixed length string without using the rand() function?
I have this but I do not want to use the rand() function
function generateRandomString($length =6) {
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($j = 0; $j < $length; $j++) {
$randomString .= $characters[mt_rand(0, $charactersLength - 1)];
}
return $randomString;
}
<?php
$start=0;$length=6;
$str='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$Val=substr(str_shuffle($str),$start,$length);
echo $Val;
?>
function mt_rand_str ($l, $c = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890') {
for ($s = '', $cl = strlen($c)-1, $i = 0; $i < $l; $s .= $c[mt_rand(0, $cl)], ++$i);
return $s;
}
Here you can call this function for 8 character
mt_rand_str(8)
you can use following code to get fixed length random string
<?php
$str = 'abcdef';
$shuffled = str_shuffle($str);
// this will genrate randome string with fixed string lenght
echo $shuffled;
?>
I'm working with this script to generate a random string:
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
I echo the function to show the user the string, but I'm not sure how to place the string in my database.
You can just put the result of the function in a variable:
$generated_string = generateRandomString();
And save it using a normal query