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
I'm trying to build a custom base that consist of 31 characters:
$custom_array = array("0","1","2","3","4","5","6","7","8","9",
"b","c","d","f","g","h","j","k","l","m","n"
,"p","q","r","s","t","v","w","x","y","z");
I excluded vowels from the alphabet and added numbers at the begining.
Well, basically i need a function that is similar to base_convert(), so that i can convert to base10 and vice versa.
Thanks for any help.
Edit:
I came up with this function but the accepted answer is also a good approach. Thanks.
$basearray = array("0","1","2","3","4","5","6","7","8","9","b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z");
function baseXto10($code,$basearray)
{
$codearr = array_reverse(str_split($code));
$charnum = count($basearray);
$sum = 0;
$i = 0;
foreach($codearr as $key => $value)
{
$charpos = array_search($basearray, $value);
$sum+= $charpos * pow($charnum,$i);
$i++;
}
return $sum;
}
You probably want to use base_convert() for converting between actual bases. Then, from base10 (or any for that matter) map the values between the initial and your new base.
Perhaps something like this
function my_base_convert($input, $fromBase)
{
$baseMapping = [
'0' => 0,
'a' => 'b',
//etc
];
$result = base_convert($input, $fromBase, 10);
return str_replace(array_keys($baseMapping), array_values($baseMapping), $input);
}
Then simple str_replace
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 5 years ago.
Improve this question
Hi am having 10 elements in array . Am trying to get my random element mostly from first 5 elements. Which means random element appearance from first 5 elements should be much greater than next 5 elements
$arr = array('a','b','c','d','e','f','g','h','i','j');
$random = $arr[array_rand($arr)];
Am using above one to get the random element normally
Use function rand(min_num, max_num):
function rand5($array) {
$part = rand(1, 10);
return ($part > 3) ? $array[rand(0, 4)] : $array[rand(5, 9)];
}
$arr = array('a','b','c','d','e','f','g','h','i','j');
$random = rand5($arr);
Try this simple and easy code :-
$arr = array('a','b','c','d','e','f','g','h','i','j');
$rand = rand(0,9);
echo $arr[($rand <= 6 ? ($rand%5) : $rand)];
You just have to get the random number between 0 to 9 and divide that number in 70%(0-6) and 30%(7-9). If its greater than 5 then only use the remainder else directly get that number
Here is the fiddle :- https://3v4l.org/TWGJJ
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 6 years ago.
Improve this question
I would like to create a website that allows a user to generate a selectable number of unique strings of text that all follow an algorithm but as it is website based I am not too sure about how I go about it.
For example user A wants to generate 20 strings of unique text that all follow say AA***B^^** where A&B is a constant that doesn't change, where * is a random number and ^ is a random letter.
Is that possible? I am thinking of using php rand for the number but not 100% sure.
Thanks
You could use something like this:
<?php
function randomGenerator($string)
{
$string_array = str_split( $string );
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
foreach ($string_array as $k => $v) {
if ($v == '*')
$string_array[$k] = rand(0,9);
if ($v == '^')
$string_array[$k] = $characters[rand(0,51)];
}
$string = implode('', $string_array);
return $string;
}
echo randomGenerator('AA***NN^^'); // may print AA478NNhU
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
I am just trying to figure out how I can get the actual digits of a figure that has been calculated within php and formatted to have 2 decimal places.
so say its calculated it to be 45.76 I am trying to figure out how I can get the 76 from it for an if statement. Basically I want it to look and just say that if it's 00 then remove them, if not, show them.
Thanks
Try :
function showDecimals($v){
$n = abs($v);
$whole = floor($n);
$fraction = $n - $whole;
return $fraction > 0
}
And...
if (showDecimals(10.15)){
//Show
}else{
//Remove?
}
You want to show a whole number if there is no decimal place, and 2 decimals of precision if not?
Method 1
function formatNumber($n) {
$n = round($n*100)/100;
return ''+$n;
}
This simply rounds it to 2 decimals of precision. Zero truncation is automatic.
Usage
echo formatNumber(0); //0
echo formatNumber(0.5); //0.5
echo formatNumber(0.894); //0.89
echo formatNumber(0.896); //0.9
echo formatNumber(1.896); //1.9
Method 2
Or if you 1.9 to display as 1.90, I suppose this would work:
function formatNumber($n) {
if ($n == 0)
return ''+$n;
$str = ''.round($n*100)/100;
$dotpos = strrpos('.', $str);
if (strlen(substr($str, $dotpos+1)) === 2)
$str .= '0';
return $str;
}
Usage:
echo formatNumber(0); //0
echo formatNumber(0.5); //0.50
echo formatNumber(0.894); //0.89
echo formatNumber(0.896); //0.90
echo formatNumber(1.896); //1.90
Edit: Accidentally posted broken version of method 2, should be fixed now.
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 have generate a 16 digit number and i want to extract one number from every 4 digits of that 16 digit number. For e.g: 1234567892345678. I want to extract 2 from 1234, 7 from 5678, 3 from 9034 & 7 from 5678. Then store it in another variable $a. the extraction will be in a random manner.
You can try this -
$d = '1234567892345678';
$s = str_split($d, 4); // split in 4 digits
$n = array_map(function($x) {
return substr($x, rand(0, 3), rand(1, 1)); // extract single digit random number
}, $s);
$n will hold the random numbers.
I might be late at answering this question but you can simply use strlen function along with for loop like as
$str = "1234567892345678";
for($i = 0; $i < strlen($str);$i += 4){
echo $str[$i+rand(0,3)];
}
Here you have a one-liner:
$s = $string[rand(0,3)].$string[rand(4,7)].$string[rand(8,11)].$string[rand(12,15)];
echo $s;
Another one-liner:
for($s='',$i=0;$i<10; $s.=$string[rand($i,$i+=3)]);
echo $s;
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 want to know how I can randomize first and last two digits in php? Suppose the current code is:
FJA793HIYX
then after randomizing it should be:
HJA793HIGD
How can I do that?
Try
$str = 'HJA793HIGD';
$filter = substr($str,1,7);
echo $newstr= randLetter().$filter.randLetter().randLetter();
function randLetter() {
$int = rand(0,26);
$alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$rand_letter = $alpha[$int];
return $rand_letter;
}
Try this!
$randomDigit = substr( "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ,mt_rand( 0 ,34 ) ,1 ) .substr( md5( time() ), 1)