This might seem like a very simple thing, and maybe I'm just not understanding the question? Anyway. $a and $b are random numbers from 1000 to 10000. I need to take the largest one and round it to the nearest hundred.
Is this supposed to be like so:
$a = 5;
$b = 10;
echo mt_rand($a,$b);
But then, how do I get the largest number and round it?
I don't know why this confuses me so much.
Also, apparently, I am supposed to use max somewhere.
You need to add the values to an array then use max of the array.
To round the value to closest 100 you need to use the following formula round(x/100)*100
This example makes 10 random numbers and echo the max value and the rounded max value.
For($i=0;$i<10;$i++){
$a[] = mt_rand(1000,10000);
}
//Var_dump($a);
Echo max($a). "\n"; // max number
Echo round(max($a)/100)*100; //max number rounded
https://3v4l.org/NRWiH
Related
I have an array and I am getting its length using sizeof(). In the size of the array I want to get how many 10 there are in that number. I used % in getting my number like:
$arraysize = 13;//sizeof($array)
$numberoften = $arraysize % 10 // getting 3
From here I know that I am wrong. What i want to get is 2. Because I want to get every 10. Like if length is 9 I want to get 1. If length is 31 I want to get 4.
What is the correct function of way to get my desired output?
What you're after is ceil()
Returns the next highest integer value by rounding up value if necessary.
$numberoften = ceil($arraysize / 10);
There's also floor() if you want to round the number down to the nearest integer. This would seem to fit better with your question...
I want to get how many 10 there are in that number
Use ceil() function
$arraysize = 13;//sizeof($array);
$numberoften =ceil( $arraysize/10);
Or
Use round() function
$arraysize = 13;//sizeof($array);
$numberoften =round( $arraysize/10);
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.
In PHP, I need to round off a number to the next whole number. For example, in my program, I am using the round as below.
$val = round(count($names)/15);
If count($names) is 1.2, I need it to be rounded off to 2 instead of 1. I tried to do the below approach.
$val = round(count($names)/15) + 1;
However, in the above approach, if I have count($names) as 1.6, It is getting rounded off to 3 as I increment it by 1.
Is there a way where no matter what the decimal value is, it needs to be rounded off to the next whole number?
How about using ceil()
http://php.net/manual/en/function.ceil.php
Then your code becomes:
$val = ceil(count($names)/15);
Try This:
$val = round(count($names)/15, PHP_ROUND_HALF_DOWN) + 1;
What would be a good way to generate 7 unique random numbers between 1 and 10.
I can't have any duplicates.
I could write a chunk of PHP to do this (using rand() and pushing used numbers onto an array) but there must be a quick way to do it.
any advice would be great.
Create an array from 1 to 10 (range).
Put it in random order
(shuffle).
Select 7 items from the array (array_slice)
Populate an array with ten elements (the numbers one through ten), shuffle the array, and remove the first (or last) three elements.
Simple one-liner:
print_r(array_rand(array_fill(1, 10, true), 7));
Check out the comments in the php manual, there are several solutions for this.
An easy one is this one:
$min = 1;
$max = 10;
$total = 7;
$rand = array();
while (count($rand) < $total ) {
$r = mt_rand($min,$max);
if (!in_array($r,$rand)) $rand[] = $r;
}
Whole numbers? Well, if you want 7 out of 10 then you more efficiently DON'T want 3 out of 10.
Feel free to use any of the other responses but instead of creating 7 numbers start with 10 and eliminate 3. That will tend to speed things up by more than double.
The "shuffle" method has a MAJOR FALW. When the numbers are big, shuffle 3 billion indexs will instantly CAUSE 500 error. Here comes a best solution for really big numbers.
function getRandomNumbers($min, $max, $total) {
$temp_arr = array();
while(sizeof($temp_arr) < $total) $temp_arr[rand($min, $max)] = true;
return $temp_arr;
}
Say I want to get 10 unique random numbers from 1 billion to 4 billion.
$random_numbers = getRandomNumbers(1000000000,4000000000,10);
PS: Execution time: 0.027 microseconds
How can I regenerate random decimal from -0.0010 to 0.0010 with php rand() or some other method?
Divide rand() by the maximum random numer, multiply it by the range and add the starting number:
<?php
// rand()/getrandmax() gives a float number between 0 and 1
// if you multiply it by 0.002 you'll get a number between 0 and 0.002
// add the starting number -0.001 and you'll get a number between -0.001 and 0.001
echo rand()/getrandmax()*0.002-0.001;
?>
.
$val = (rand(0,20)-10)/10000;
This uses two rand() calls but I think that the readability makes up for it tenfold.
The first part makes either a -1 or +1. The second part can be anything between 0 and your limit for +/- numbers.
$rand = (rand(0,1)*2-1)*rand(0, 100);
echo $rand;
Unless you require LOTs of random numbers in a gigantic loop, you probably won't even notice the speed difference. I ran some tests (50.000 iterations) and it came up to around 0.0004 milliseconds to get a random number by my function. The alternatives are around half that time, but again, unless you are inside a really big loop, you are probably better of optimizing somewhere else.
Speed testing code:
$start = microtime();
$loopCount = 50000;
for($i=0;$i<$loopCount;$i++)
{
(0*2-1)*rand(0, 100);
}
$end = microtime();
echo "Timing: ", ((($end-$start)*1000.0)/((float)$loopCount)), " milliseconds.";
This will return any possible number between -0.001 and +0.001
$random = ((rand()*(0.002/getrandmax()))-0.001)
// or without paranthesis:
$random = rand()*0.002/getrandmax()-0.001
$randselect=rand(0,(array_sum($adarray)*100000000));
$cumilativevalue=0;
foreach ($adarray as $key => $value) {
$cumilativevalue=$cumilativevalue+$value*100000000;
if($randselect<$cumilativevalue){$selectedad=$key;break;}
}
Random float with one decimal between -1,1
$random = round((rand(0,1) - floatVal('0.'.rand(0,9).rand(0,9))), 1);