Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to generate 34 random letters (capitalized and not) and numbers. Before the generated 34 random alphanumerics there is a word pro_sec_ which is there everytime it generate. And the 25th & 26th is 00
So the generated result will be: (just an example)
pro_sec_Vy6Q67sh6HvP90j7gWnq6SxN00p8Gapu66
Another ex.
pro_sec_6B57nTshusbnay6esjabxns800nGvaz2Lov
The pro_sec_ and the 00 there in 25th and 26th are always the same and the rest are just generated.
This should work for you:
<?php
function getToken($n) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $n; $i++) {
$index = rand(0, strlen($characters) - 1);
$randomString .= $characters[$index];
}
return $randomString;
}
echo 'pro_sec_' . getToken(24) . '00' . getToken(9);
Performant solution without using any loops or calling the function twice:
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$split = str_split(substr(str_shuffle($chars), 0, 32), 25);
$res = 'pro_sec_'.$split[0].'00'.$split[1];
echo $res; // pro_sec_c8s5iwyQY0nZoJf73EOkC9vqS00dNLWVKg
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to get a number between 1 to 6 with more chances to be close to 1.
i have tried this:
<li>{{Faker\Factory::create()->biasedNumberBetween($min = 10, $max = 20, $function = 'unbiased')}}</li>
What i am trying to do is to generate a number from 1 to 6 rand(1,6); but make the numbers be closer to one as the lower numbers will have more weight than the others.
Something like this ?
<?php
function weightedRand($min, $max, $weightedMax) {
$arr = array();
for($i = 0; $i < 10; $i++) {
$arr[] = rand($min, $weightedMax);
}
$arr[] = rand($min, $max);
return $arr[rand(0,10)];
}
echo weightedRand(1,6, 3);
?>
numbers below 4 will now be more likely than numbers above :)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How do I generate random 12 character char for URL of a page for the record in the database like how youtube does it with 11 characters https://www.youtube.com/watch?v=kDjirnJcanw
I want it to be unique for each entry in the database and consist of letters of upper and lower case, numbers and maybe (if not bad for security) even special characters.
Function:
function generateRandomString($length = 12) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
Then call it:
$randomString = generateRandomString();
Slightly adapted from Stephen Watkins' solution here
This is a pretty naive approach, but you could define an array of allowed characters, and just randomly select N:
function randomString($length) {
$string = '';
$characters = "123456789abcdefghijklmnopqrstuvwxyzABCDEFHJKLMNPRTVWXYZ";
$randMax = strlen($characters)-1;
for ($i = 0; $i < $length; ++$i) {
$string .= $characters[mt_rand(0, randMax)];
}
return $string;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have long decimal numbers that I'd like to make easier to read. There's many 0's involved and I would like to bold the end of the string from where the numbers stop being just 0's. The number will always have 8 decimal places, no more, no less. It's worth noting that I would like any trailing zeros to also be bold, I don't just want to avoid bolding 0 as a whole.
Example:
Before - 0.00004320
After - 0.00004320
Any help or pointers in the right direction much appreciated.
Thanks.
You can do it using a loop and some string manipulation:
function doTheThing($number) {
$length = strlen($number);
$period = strpos($number, '.') + 1;
for ($i = $period; $i < $length; $i++) {
if ($number[$i] !== '0') {
$number = substr($number, 0, $i).'<b>'.substr($number, $i).'</b>';
break;
}
}
return $number;
}
echo doTheThing('0.00004320');
Or you can do it using a regular expression replacement:
function doTheThing($number) {
return preg_replace('/^(\d+\.0*)(\d+)$/', '$1<b>$2</b>', $number);
}
echo doTheThing('0.00004320');
echo doTheThing(sprintf('%.8f', 0.00004320)); // if your number is not a string
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am testing a php function i found on the php documentation based on this comment:
http://www.php.net/manual/en/function.rand.php#108861
<?php
function RandNumber($e) {
for ($i = 0; $i < $e; $i++) {
$rand = $rand . rand(0, 9);
}
return $rand;
}
echo RandNumber(4);
// Outputs a 6 digit random number
?>
I get the error Notice:
Undefined variable: rand in /var/www/eod.php on line 7
This is line 7:
$rand = $rand . rand(0, 9);
Why is causing this error since the function works as expected?
Initialize your $rand variable to remove this warning :
function RandNumber($e){
$rand="";
for($i=0;$i<$e;$i++){
$rand = $rand.rand(0, 9);
}
return $rand;
}
Note that you may simplify your code :
function RandNumber($e){
$rand="";
for(;$e-->0;){ // no need for an additional variable
$rand .= rand(0, 9); // addition and assignement with one operator
}
return $rand;
}
To output a random 6 digit number (If you need larger numbers, greater than mt_getrandmax(), this solution would fail), simply use
mt_rand(100000, 999999);
To include numbers with leading zeros (000123), you can use a combination of my_rand(0, 999999) and str_pad($number, 6, "0", STR_PAD_LEFT);
No need for fancy looping.
This is not an error.. it's a notice message.. If you don't want to appear that notice you should define it before doing the loop:
$rand = '';
for($i=0;$i<$e;$i++){
$rand = $rand . rand(0, 9);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Suppose I have a string such as this:
ABC.DEF.GHI.JKL (This string could have any length and amount of characters between dots)
I want to add the following combinations into an array.
ABC
ABC.DEF
ABC.DEF.GHI
ABC.DEF.GHI.JKL
So basically the string should be split with the dot character and then the individual sub-strings should be combined.
I have a function already, but in my opinion this seems too complicated to achieve the result.
$myarray = array();
$split = explode('.', 'that.string.with.dots');
$string = '';
for ($i = 0; $i < count($split); $i++) {
$string .= $split[$i];
myarray[] = $string;
$string .= '.';
}
Any suggestions on improving this?
Thanks
Michael
Perhaps this:
$split = explode('.', 'that.string.with.dots');
for ($i = 1; $i < count($split); $i++) {
$split[i] = $split[$i-1] . '.' . $split[i];
}
It just concats the current with the previous.
I have the feeling that you are asking if there is a PHP function that will achieve this result with a single function call, but I don't think there is one.
You can rewrite the code as pritaeas did in his answer, to remove two lines of code, but it will still be the same concept.