generating random integer with 10 numbers always start with 1 - php

i am using this code
<?php
function random()
{
return rand(1111111111,9999999999);
};
for ($x = "1";$x <= "5";$x++)
{
echo $x." : ".random()."<br>";
};
echo "<hr>";
?>
some outputs :
1 : 1303960718
2 : 1308203081
3 : 1280148745
4 : 1263151923
5 : 1124814399
i tried generating more numbers and all of it starts with 1
i tried to used rand() directly and the same thing happend

Run this code and you will get your answer yourself
return rand(2147483647,9999999999);
Then try running
echo getrandmax();
Depending upon your system you might get something like 2147483647
That means your upper limit is pretty much useless beyond that number. And on certain systems that max can even be lower than that. You also have to research about integer overflow.
Now if you were to go easy on your system and remove 1 digit from your number and make the new range
return rand(111111111,999999999);
Then your code would work just fine, because there are no overflows.

Related

Same algorithm runs differently in C and PHP

I was practicing a problem on CodeChef.com.
https://www.codechef.com/problems/CHOPRT --> Link to the question I was solving.
I successfully solved the question using C.
My C code:
#include <stdio.h>
int main() {
int t;
int num1;
int num2;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &num1, &num2);
if(num1 > num2)
printf(">");
else if(num1 < num2)
printf("<");
else
printf("=");
printf("\n");
}
return 0;
}
But I am not able to solve it using PHP:
My PHP code:
<?php
$t = intval(fgets(STDIN));
while($t--) {
$line = split(" ", trim(fgets(STDIN)));
$num1 = intval($line[0]);
$num2 = intval($line[1]);
if($num1 < $num2)
print("<");
else if($num1 > $num2)
print(">");
else
print("=");
print("\n");
}
?>
Though both the programs run perfectly on my MacBook Pro, the PHP code is not run on codechef.com and gives a WA(Wrong Answer). C code is run perfectly though and that to within 0.00 seconds.
Please, enlighten me with the difference between the two respective codes, which I personally believe, should be working the same, and also produce the same output.
Often i hear that some testcases are erroneous.I have a corner case for you which would give perfect result for your C code but not for your PHP
Do this :
1
10 10
Notice there is more than one space between the two digits.
I tested it here.
Instead of the expected output which is:
=
your Output is:
>
Though C would pass this test case as scanf searches for the next integer number you type, PHP would fail since there is more than one space.
To make it work in PHP i did suggest you to code in such a way that the spaces between the two numbers dont affect your expected output.
That's the only way i believe your PHP Code won't work.If this indeed was the issue it's not your fault!

PHP how to show all decimal places?

this might be a stupid question but I have searched again and again without finding any results.
So, what I want is to show all the decimal places of a number without knowing how many decimal places it will have. Take a look at this small code:
$arrayTest = array(0.123456789, 0.0123456789);
foreach($arrayTest as $output){
$newNumber = $output/1000;
echo $newNumber;
echo "<br>";
}
It gives this output:
0.000123456789
1.23456789E-5
Now, I tried using 'number_format', but I don't think that is a good solution. It determines an exact amount of decimal places, and I do not know the amount of decimal places for every number. Take a look at the below code:
$arrayTest = array(0.123456789, 0.0123456789);
foreach($arrayTest as $output){
$newNumber = $output/1000;
echo number_format($newNumber,13);
echo "<br>";
}
It gives this output:
0.0001234567890
0.0000123456789
Now, as you can see there is an excess 0 in the first number, because number_format forces it to have 13 decimal places.
I would really love some guidance on how to get around this problem. Is there a setting in PHP.ini which determines the amount of decimals?
Thank you very much in advance!
(and feel free to ask if you have any further questions)
It is "impossible" to answer this question properly - because a binary float representation of a decimal number is approximate: "What every computer scientist should know about floating point"
The closest you can come is write yourself a routine that looks at a decimal representation of a number, and compares it to the "exact" value; once the difference becomes "small enough for your purpose", you stop adding more digits.
This routine could then return the "correct number of digits" as a string.
Example:
<?php
$a = 1.234567890;
$b = 0.123456789;
echo returnString($a)."\n";
echo returnString($b)."\n";
function returnString($a) {
// return the value $a as a string
// with enough digits to be "accurate" - that is, the value returned
// matches the value given to 1E-10
// there is a limit of 10 digits to cope with unexpected inputs
// and prevent an infinite loop
$conv_a = 0;
$digits=0;
while(abs($a - $conv_a) > 1e-10) {
$digits = $digits + 1;
$conv_a = 0 + number_format($a, $digits);
if($digits > 10) $conv_a = $a;
}
return $conv_a;
}
?>
Which produces
1.23456789
0.123456789
In the above code I arbitrarily assumed that being right to within 1E-10 was good enough. Obviously you can change this condition to whatever is appropriate for the numbers you encounter - and you could even make it an optional argument of your function.
Play with it - ask questions if this is not clear.

mt_rand max value

On the php documentation,i found this note:
On both 32 and 64-bit systems (OS X and Linux), mt_getrandmax()
returns 2147483647
I have confirmed this using the simple function provided
function gethighest()
{
return mt_getrandmax();
}
$hello = gethighest();
echo '<b>'.$hello.'</b>';
I am using this snippet to generate a unique id
$number = mt_rand(163245,978534);
$unique_id = crypt($number);
echo md5($unique_id).'<br/>';
My question is,what does it mean to have a max value for mt_rand?.Will the ids begin to repeat once the max value is reached?.
Your code:
$number = mt_rand(163245,978534);
means that the number generated will be between those 2 numbers, it will for as many times as you run it, generate a number between those 2 values.. No returned value will be outside that range. But yes, values can be repeated..
eg try it with mt_rand(1,5) and tell it to do it 20 times and output.
Consider using uniqid? http://php.net/manual/en/function.uniqid.php
If you intent to generate a unique ID, you should use uniqid:
$unique_id = uniqid(microtime(true));

How to write a function to Generate String like A0001, A0002, ..A9999,... AA0001, AA9999,... AZ0001, AZ9999,

How to write a function in PHP to Generate String like
A0001, A0002, ..A9999,... AA0001, AA9999,... AZ0001, AZ9999,...
Also if its possible to write a function in mysql please let me to know.
I am going to Define the Unique ID for records in database,
so i need to generate Unique ID.
right now i am using auto increment on table. now its touch 8 digit, so i need to minimize it with 4 to 6 digits. one of my friend said use this formula so you will minimize your length of unique id.
A9999 = 9,999 Records
AA0001 to AZ9999 = 259,974 Records
AA0001 to ZZ9999 = 175,742,424 Records
or else if you have any other ideas, please give it to me.
Thanks in advance.
GOT SOLUTION FOR MY ISSUE
Thanks to your suggestion and idea, with #thecoshman helps i solved my issues Virtually.
function encodeItemBarcode($number) {
$number = base_convert($number, 10, 36);
$number = strtoupper($number);
return $number;
}
function decodeItemBarcode($number) {
$number = base_convert($number, 36, 10);
$number = strtoupper($number);
return $number;
}
Output:
999999999991 Encrypted as : CRE66I9J - Decrypted as : 999999999991
999999999992 Encrypted as : CRE66I9K - Decrypted as : 999999999992
999999999993 Encrypted as : CRE66I9L - Decrypted as : 999999999993
999999999994 Encrypted as : CRE66I9M - Decrypted as : 999999999994
999999999995 Encrypted as : CRE66I9N - Decrypted as : 999999999995
999999999996 Encrypted as : CRE66I9O - Decrypted as : 999999999996
999999999997 Encrypted as : CRE66I9P - Decrypted as : 999999999997
999999999998 Encrypted as : CRE66I9Q - Decrypted as : 999999999998
999999999999 Encrypted as : CRE66I9R - Decrypted as : 999999999999
So Finally i minimized 12 Digit(999999999991) into 8 Char(CRE66I9J). I am not going to store it in database, i am going to use this in Front End only.
Once again Thanks to All.
I think you could just get away with displaying the decimal ID number as a hex value, so if you had the value 3397 it would display in it's hex form of D45. With this sytem, a maximum of 6 hex digits, FFFFFF would equal a decimal value of 16777215
That would be the limit of Using hex and only displaying 6 digits.
If you need to be able to 'represent' larger numbers, I can provide a solution. Though the easiest one I can think of would be something like using base 36, ie 0 to 1 is 0 to 9, and then it continues, like hex, A to Z is 10 to 35. such the Z + 1 = A0 or 36. It will take a bit for me to think this out, so I don't want to bother if it is not what you need, just let me know via comment

How to convert an integer value to word format using php5?

I want to convert a number to word format.
For example:
$count = 5;
echo "Hello Mr user this is your ".$count." review."
I need output like this...
"Hello Mr user this is your fifth review."
Check this in another StackOverflow thread:
Convert a number to its string representation
There is no built in function in php to make that. But try external lib like : http://pear.php.net/package/Numbers_Words
If you're referring to a built-in function, no, PHP does not have one. But You can create your own.
If you're looking at only English numbers, it's not much of an issue. But if you need to deal with a foreign language (like Arabic), you have to do a bit of extra work since numbers and objects have genders. So the algorithm gets a little more complex.
Just for the record, I'm working on producing an open source conversion tool for Arabic.
I know this isn't exactly what you're after, but I picked this up from the comments on php.net somewhere (can't find source), it works for output like 1st, 243rd and 85th.
function ordinalize($num)
{
if ( ! is_numeric($num)) return $num;
if ($num % 100 >= 11 and $num % 100 <= 13)
{
return $num."th";
}
elseif ( $num % 10 == 1 )
{
return $num."st";
}
elseif ( $num % 10 == 2 )
{
return $num."nd";
}
elseif ( $num % 10 == 3 )
{
return $num."rd";
}
else
{
return $num."th";
}
}
You might want to even consider this format anyways for readability and simplicity, depending on how high you expect the numbers to be. If they are less than 100, you should be able to write your own function easily. However, if they can get really high:
"Hello Mr user this is your three thousand four hundred and twenty-fifth review."
Sounds a bit awkward :)
Hope this helps some.

Categories