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);
}
Related
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
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
Want to divide random integer number into equal five parts. And insert those values into array. Can any one tell me the logic for that.
Eg. I have 15 as my number. After dividing. It should generate the array as below.
$myArray = array('3','6','9','12','15');
Thanks in advance.
function getParts($number, $parts)
{
return array_map('round', array_slice(range(0, $number, $number / $parts), 1));
}
print_r(getParts(15, 5));
Explanation: range() generates the array of values starting with 0, ending when it reaches $number and using the step $number/$parts. It will get $parts+1 floating point numbers. array_slice() removes the first item (which is always 0). array_map() applies the function round() to each element to get the nearest integer.
Divide and create a loop to fill the array...
$total = 15;
$divide = 5;
$base = $total / $divide;
$arr = array();
for($i = 1; $i <= $divide; $i++) {
$arr[] = round($i * $base);
}
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 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
I am creating an incrementing number starting with 1001. If the number goes 1001,1002,1003... when it reaches 10, will it be formatted like 1010 or will it be 10010? I need it to just go in order and be 1010 and when it reaches 100, 1100.
$prefix = "1"; // update the prefix here
$number = 1;
$number++;
$unique = str_pad($number, 3, "0", STR_PAD_LEFT);
$unique = $prefix . $unique;
print_r($unique);
When your count reaches 10, the number printed will be 1010. As described here, str_pad "Pads a string to a certain length with another string" You can create a test with the following:
$prefix = "1"; // update the prefix here
$number = 1;
for ($number = 1; $number <= 100; $number++)
{
$unique = str_pad($number, 3, "0", STR_PAD_LEFT);
$unique = $prefix . $unique;
print($unique."\n");
}
When your count reaches 100, the number printed will be 1100.
However, if you were to go up to 1000, 11000 would be printed - str_pad apparently will not truncate the string to match the specified size.
It will be 1010, but you can test this yourself easily:
$prefix = "1"; // update the prefix here
$number = 9;
$number++;
$unique = str_pad($number, 3, "0", STR_PAD_LEFT);
$unique = $prefix . $unique;
print_r($unique); // 1010
The second argument of str_pad specifies padding. If padding is 3, then 1 becomes 001, 10 becomes 010, 100 becomes 100.
With your code it will be 10010.
It looks like you are making this more complicated than it needs to be. Why not just start with $number = 1001 and increment it and then turn it into a string?
$number = 1001;
$number++;
$unique = strval($number);
print_r($unique);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I think the subject hits it better than I was expecting.
I need a function which returns random numbers which result in a given value (X) when they are summed up.
Something like this:
getRandomTo(10); // result for example: array(2,3,5)
getRandomTo(10); // result for example: array(4,3,3)
getRandomTo(12); // result for example: array(5,1,6)
I could not find a generic algorithm function for solving that requirement. Further more I cannot imagine a FAST and performant way to create something like this my self.
Please help me out
function getRandomTo($num)
{
$x = Array();
$i = 0;
do
{
$x[$i] = rand(1,$num);
$num = $num - $x[$i];
$i++;
}while($num > 0);
print_r($x);
}
Maybe do this:
create a random value between 0 and X; say this one is called r1. save this in your array.
create another random value between 0 and (X-r1), name it r2. save it also.
do these steps as often as you need it (or as long as r1+...+rn is lower than X)
Another solution:
function randomTo($numIn) {
$numOut = 0;
$numbers = array();
do {
$add = rand(1, $numIn);
if($numOut + $add > $numIn)
continue;
$numOut += $add;
$numbers[] = $add;
} while( $numOut != $numIn );
return $numbers;
}
$result = randomTo(15);
var_dump($result);