I have this math assignment that I should make into code.
I've tried all I thought of but I couldn't find a solution.
All this should be done without using php functions, only math operations.
You can use while, for, and such...
So I have number for example 9
Now I should create number of the length 9 which would be 999999999
If I had, for example, number 3, then the result should be 333.
Any ideas?
$gen = -1;
while($highest > 0) {
$gen = $highest + ($highest * 10);
$highest = $highest - 1;
}
echo $gen;
Here is a method that does not build a string; it uses pure math. (There will be many, many ways to do this task)
$x=9;
$result=0;
for($i=$x; $i; --$i){ // this looping expression can be structured however you wish potato-potatoe
$result+=$x*(10**($i-1)); // x times (10 to the power of (i-1))
}
echo $result;
// 999999999
*note: ** acts like pow() if you want to look it up.
Late edit: here is a clever, little loopless method (quietly proud). I am only calling range() and foreach() to demo; it is not an integral component of my method.
Demo: https://3v4l.org/GIjfG
foreach(range(0,9) as $n){
// echo "$n -> ",(integer)(1/9*$n*(10**$n)-($n/10)),"\n";
// echo "$n -> ",(1/9*$n*(10**$n)-(1/9*$n)),"\n";
// echo "$n -> ",(int)(1/9*10**$n)*$n,"\n";
// echo "$n -> ",(int)(10**$n/9)*$n,"\n";
echo "$n -> ",(10**$n-1)/9*$n,"\n";
}
Output:
0 -> 0
1 -> 1
2 -> 22
3 -> 333
4 -> 4444
5 -> 55555
6 -> 666666
7 -> 7777777
8 -> 88888888
9 -> 999999999
1/9 is the hero of this method because it generates .111111111(repeating). From this float number, I am using 10**$n to "shift" just enough 1s to the left side of the decimal point, then multiplying this float number by $n, then the float must be converted to an integer to complete.
Per #axiac's comment, the new hero is 10**$n-1 which generates a series of nines to the desired length (no float numbers). Next divide the nines by nine to generate a series of ones which becomes the perfect multiplier. Finally, multiply the series of ones and the input number to arrive at the desired output.
There are two operations you need to accomplish:
given a number $number, append the digit $n to it;
repeat operation #1 some number of times ($n times).
Operation #1 is easy:
$number = $number * 10 + $n;
Operation #2 is even easier:
for ($i = 0; $i < $n; $i ++)
What else do you need?
Initialization of the variable used to store the computed number:
$number = 0;
Put them in order and you get:
// The input digit
// It gives the length of the computed number
// and also its digits
$n = 8;
// The number we compute
$number = 0;
// Put the digit $n at the end of $number, $n times
for ($i = 0; $i < $n; $i ++) {
$number = $number * 10 + $n;
}
// That's all
If intval() is accepted:
$result = '';
$input = 9;
for($i=0; $i < $input; $i++){
$result .= $input;
}
$result = intval($result);
else:
$result = 0;
$input = 9;
for($i=0; $i < $input; $i++){
$factor = 1;
for($j = 0; $j < $i; $j++){
$factor *= 10;
}
$result += $input * $factor;
}
=>
9 + 90 + 900 + 9000 + 90000...
I've got a long string consisting of HEX-characters:
$hex = '004d41494e0000000048001B030C1A050EEFF500'.
'F5FB0000400804FBF101F1F201F2F301F3FA00FA' .
'48454c4c4fFA00FAFB0008400804FBF101F1F201F2F30' .
'2F3574f524c44EF';
From this string I have to calculate a 'verify' by adding up the bytes within this string. And afterwards perform a NOT-operation on it.
In the documentation (this is a string to control a serial device) the verify should be 1C5B and after the NOT it should be E3A4
As I'm completely new to HEX I've got no clue how to tackle this 'problem'. I found a way to perform a NOT operation in php: ~ . But how to sum up the data in hex and end up with 1C5B.....
$hex_array = str_split($hex, 2);
foreach ($hex_array as $byte) {
$sum += hexdec($byte);
}
echo "Sum: ", $sum, "\n";
echo dechex($sum);
EDIT
Explanation:
Assuming the input string has even number of characters we can start from the first byte and take two hex characters (representing one byte). That's what str_split does. Then we loop over each of that two-character substrings and parse them to number using dechex that we then add to the main sum. After that the only thing left is to represent that sum as hex number, using hexdec.
References from php.net:
array str_split ( string $string [, int $split_length = 1 ] )
string dechex ( int $number )
number hexdec ( string $hex_string )
<?php
$hex = '004d41494e0000000048001B030C1A050EEFF500'.
'F5FB0000400804FBF101F1F201F2F301F3FA00FA' .
'48454c4c4fFA00FAFB0008400804FBF101F1F201F2F30' .
'2F3574f524c44EF';
$n = strlen($hex);
$sum = 0;
for ($i = 0; $i < $n; $i+= 2)
{
//fetch 2 chars from string
$byte = $hex[$i].$hex[$i + 1];
$byte = intval($byte, 16);
$sum += $byte;
}
//0x1C5B;
print sprintf('0x%4X', $sum)."\n";
$sum = ~$sum;
//To avoid the problem: prints FFFFE3A4, and not E3A4
$sum = $sum & 0xFFFF;
//E3A4
print sprintf('0x%4X', $sum)."\n";
How can you convert a integer into it's bits representation?
For example number 9 representation in bits is: 10011
For example to convert a bit sequence into it's int representation you can do this:
$bits_sq = array(1,0,0,1,1);
function convert_bits_to_int($bits_sq){
$sum = 0;
for($i=0; $i < count($bits_sq); $i++){
$sum = $sum + $bits_sq[$i] * pow(-2, $i);
}
print $sum; // equals to 9
}
But I want the other way around.
Edit: DO NOT MISTAKE BITS WITH BINARY, THIS IS NOT THE DUPLICATE NEITHER HAS THE ANSWER IN THE ABOVE THREAD
You need to use decbin() to convert an integer to binary.
http://php.net/manual/en/function.decbin.php
My php is rusty however if you want to do the reverse of this example
$bits_sq = array(1,0,0,1,1);
function convert_bits_to_int($bits_sq){
$sum = 0;
for($i=0; $i < count($bits_sq); $i++){
$sum = $sum + $bits_sq[$i] * pow(-2, $i);
}
print $sum; // equals to 9
}
then I suppose you want something like:
$bits_sq = convert_int_to_bits ($iValue);
function convert_int_to_bits ($iValue) {
$bits = array(); // initialize the array
do {
$bits[] = ($iValue & 1);
$iValue >>= 1; // shift the bit off so that we go to the next one
} while ($iValue); // continue as long as there are still some bits.
// we have the bits in reverse order so lets reverse it.
return array_reverse($bits);
}
Just dropping this here.
echo str_pad(decbin(1), 4, '0', STR_PAD_LEFT); // 0001
echo str_pad(decbin(7), 4, '0', STR_PAD_LEFT); // 0111
echo str_pad(decbin(9), 4, '0', STR_PAD_LEFT); // 1001
need a little help with a small issue of string splitting.
I'm trying to split a serial number into two, do some calculations on the second half and join it back to the first half. My problem is the second half starts with two zeros and PHP removes the leading zeros.
I think keeping the variables as strings will keep the zeros but I can't seem to find a way to split the serial number into smaller strings, all the methods I try split them into an array. Here is a part of my code;
$info1 = nkw549blc003i00021; //this is the serial number.
I want to split $info1 into;
$number1 = nkw549blc003i0
$number2 = 0021
then use for loop on $number2 like
$num = 1;
for ($num=1; $num < $unitsquantity[$key] ; $num++) {
$sum = $number2+$num;
$final=$number1.$sum;
echo "$final<br>";
}
Any help is greatly appreciated.
$info1 = 'nkw549blc003i00021';
$number1 = substr($info1, 0, -4);
$number2 = sprintf('%1$04d', substr($info1, -4, 4));
If the string will always be 4 chars long, you can use str_pad
for ($num=1; $num < $unitsquantity[$key] ; $num++) {
echo $number1 . str_pad($number2+$num, 4, '0', STR_PAD_LEFT);
}
Strings are array chars, so you can get each char of them by iterating through their length
define('SERIAL_NUM_LEN', 4);
$info1 = 'nkw549blc003i00021';
$number1 = ''; $number2 = '';
for ($i = 0; $i < strlen($info1)-SERIAL_NUM_LEN; $i++) {
$number1 .= $info1[$i];
}
for ($i = strlen($info1)-SERIAL_NUM_LEN; $i < strlen($info1); $i++) {
$number2 .= $info1[$i];
}
var_dump($number1, $number2);
Output:
string 'nkw549blc003i0' (length=14)
string '0021' (length=4)
This way you can skip whichever chars from the string you want if you want to build totally different string. Or add chars in the middle.
I need to create a random number with x amount of digits.
So lets say x is 5, I need a number to be eg. 35562
If x is 3, then it would throw back something like; 463
Could someone show me how this is done?
You can use rand() together with pow() to make this happen:
$digits = 3;
echo rand(pow(10, $digits-1), pow(10, $digits)-1);
This will output a number between 100 and 999. This because 10^2 = 100 and 10^3 = 1000 and then you need to subtract it with one to get it in the desired range.
If 005 also is a valid example you'd use the following code to pad it with leading zeros:
$digits = 3;
echo str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT);
I usually just use RAND() http://php.net/manual/en/function.rand.php
e.g.
rand ( 10000 , 99999 );
for your 5 digit random number
Here is a simple solution without any loops or any hassle which will
allow you to create random string with characters, numbers or even with special symbols.
$randomNum = substr(str_shuffle("0123456789"), 0, $x);
where $x can be number of digits
Eg.
substr(str_shuffle("0123456789"), 0, 5);
Results after a couple of executions
98450
79324
23017
04317
26479
You can use the same code to generate random string also, like this
$randomNum=substr(str_shuffle("0123456789abcdefghijklmnopqrstvwxyzABCDEFGHIJKLMNOPQRSTVWXYZ"), 0, $x);
Results with $x = 11
FgHmqpTR3Ox
O9BsNgcPJDb
1v8Aw5b6H7f
haH40dmAxZf
0EpvHL5lTKr
You can use rand($min, $max) for that exact purpose.
In order to limit the values to values with x digits you can use the following:
$x = 3; // Amount of digits
$min = pow(10,$x);
$max = pow(10,$x+1)-1);
$value = rand($min, $max);
Treat your number as a list of digits and just append a random digit each time:
function n_digit_random($digits) {
$temp = "";
for ($i = 0; $i < $digits; $i++) {
$temp .= rand(0, 9);
}
return (int)$temp;
}
Or a purely numerical solution:
function n_digit_random($digits)
return rand(pow(10, $digits - 1) - 1, pow(10, $digits) - 1);
}
the simplest way i can think of is using rand function with str_pad
<?php
echo str_pad(rand(0,999), 5, "0", STR_PAD_LEFT);
?>
In above example , it will generate random number in range 0 to 999.
And having 5 digits.
function random_numbers($digits) {
$min = pow(10, $digits - 1);
$max = pow(10, $digits) - 1;
return mt_rand($min, $max);
}
Tested here.
rand(1000, 9999); works more faster than x4 times rand(0,9);
benchmark:
rand(1000, 9999) : 0.147 sec.
rand(0,9)x4 times : 0.547 sec.
both functions was running in 100000 iterations to make results more explicit
Well you can use as simple php function mt_rand(2000,9000) which can generate a 4 digit random number
mt_rand(2000,9000)
You can generate any x-digit random number with mt_rand() function.
mt_rand() is faster than rand().
Syntax : mt_rand() or mt_rand($min , $max).
Example : <?php echo mt_rand(); ?>
read more
do it with a loop:
function randomWithLength($length){
$number = '';
for ($i = 0; $i < $length; $i++){
$number .= rand(0,9);
}
return (int)$number;
}
rand or mt_rand will do...
usage:
rand(min, max);
mt_rand(min, max);
function random_number($size = 5)
{
$random_number='';
$count=0;
while ($count < $size )
{
$random_digit = mt_rand(0, 9);
$random_number .= $random_digit;
$count++;
}
return $random_number;
}
Following is simple method to generate specific length verification code. Length can be specified, by default, it generates 4 digit code.
function get_sms_token($length = 4) {
return rand(
((int) str_pad(1, $length, 0, STR_PAD_RIGHT)),
((int) str_pad(9, $length, 9, STR_PAD_RIGHT))
);
}
echo get_sms_token(6);
this simple script will do
$x = 4;//want number of digits for the random number
$sum = 0;
for($i=0;$i<$x;$i++)
{
$sum = $sum + rand(0,9)*pow(10,$i);
}
echo $sum;
This is another simple solution to generate random number of N digits:
$number_of_digits = 10;
echo substr(number_format(time() * mt_rand(),0,'',''),0,$number_of_digits);
Check it here: http://codepad.org/pyVvNiof
function rand_number_available($already_mem_array,$boundary_min,$boundary_max,$digits_num)
{
$already_mem_array_dim = count($already_mem_array); // dimension of array, that contain occupied elements
// --- creating Boundaries and possible Errors
if( empty($digits_num) ){
$boundary_dim = $boundary_max - $boundary_min;
if($boundary_dim <= 0){
$error = -1; // Error that might happen. Difference between $boundary_max and $boundary_min must be positive
}else{
$error = -2; // Error that might happen. All numbers between, $boundary_min and $boundary_max , are occupied, by $already_mem_array
}
}else{
if($digits_num < 0){ // Error. If exist, $digits_num must be, 1,2,3 or higher
$error = -3;
}elseif($digits_num == 1){ // if 'one-figure' number
$error = -4; // Error that might happen. All 'one-figure' numbers are occupied, by $already_mem_array
$boundary_min = 0;
$boundary_max = 9;
$boundary_dim = $boundary_max-$boundary_min;
}elseif($digits_num == 2){ // if 'two-figure' number
$error = -5; // Error that might happen. All 'two-figure' numbers are occupied, by $already_mem_array
$boundary_min = 10;
$boundary_max = 99;
$boundary_dim = $boundary_max-$boundary_min;
}elseif($digits_num>2){ // if 'X-figure' number. X>2
$error = -6; // Error that might happen. All 'X-figure' numbers are occupied, by $already_mem_array. Unlikely to happen
$boundary_min = pow(10, $digits_num-1); // stepenovanje - graduation
$boundary_max = pow(10, $digits_num)-1;
$boundary_dim = $boundary_max-$boundary_min;
}
}
// -------------------------------------------------------------------
// --- creating response ---------------------------------------------
if( ($already_mem_array_dim <= $boundary_dim) && $boundary_dim>0 ){ // go here only if, there are AVAILABLE numbers to extract, and [difference] $boundary_dim , is positive
do{
$num = rand($boundary_min,$boundary_max);
}while( in_array($num, $already_mem_array) );
$result = $num;
}else{
$result = $error; // Limit that happened
}
return $result;
// -------------------------------------------------------------------
}
This function works perfectly with no repeats and desired number of digits.
$digits = '';
function randomDigits($length){
$numbers = range(0,9);
shuffle($numbers);
for($i = 0; $i < $length; $i++){
global $digits;
$digits .= $numbers[$i];
}
return $digits;
}
You can call the function and pass the number of digits for example:
randomDigits(4);
sample results:
4957 8710 6730 6082 2987 2041 6721
Original script got from this gist
Please not that rand() does not generate a cryptographically secure value according to the docs:
http://php.net/manual/en/function.rand.php
This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using random_int(), random_bytes(), or openssl_random_pseudo_bytes() instead.
Instead it is better to use random_int(), available on PHP 7 (See: http://php.net/manual/en/function.random-int.php).
So to extend #Marcus's answer, you should use:
function generateSecureRandomNumber($digits): int {
return random_int(pow(10, $digits - 1), pow(10, $digits) - 1);
}
function generateSecureRandomNumberWithPadding($digits): string {
$randomNumber = random_int(0, pow(10, $digits) - 1);
return str_pad($randomNumber, $digits, '0', STR_PAD_LEFT);
}
Note that using rand() is fine if you don't need a secure random number.
The following code generates a 4 digits random number:
echo sprintf( "%04d", rand(0,9999));
you people really likes to complicate things :)
the real problem is that the OP wants to, probably, add that to the end of some really big number. if not, there is no need I can think of for that to be required. as left zeros in any number is just, well, left zeroes.
so, just append the larger portion of that number as a math sum, not string.
e.g.
$x = "102384129" . complex_3_digit_random_string();
simply becomes
$x = 102384129000 + rand(0, 999);
done.