Generating a random number between several intervals in php [closed] - php

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 2 years ago.
Improve this question
I'd like to create a random number between several intervals in php. So I have several intervals in my database and I would like to randomly generate a number belonging to one of these intervals.
I have already thought of selecting a random interval and then generating a random number within that interval but I think there are much simpler and optimized methods.
Thank you for your help!

You can use random_int()
function random_int($min, $max) { }
#param int $min
The lowest value to be returned, which must be PHP_INT_MIN or higher.
#param int $max
The highest value to be returned, which must be less than or equal to PHP_INT_MAX.
#return int
Returns a cryptographically secure random integer in the range min to max, inclusive.
random_int($min, $max)
Assuming you already have $intervals list, you can loop through it.
foreach($intervals as $interval) {
random_int($interval['min'], $interval['max']);
}

$numbers = range(1, 20);
shuffle($numbers);

Related

How should I correct ill-formatted lat-long pairs to be valid for PHP computation [closed]

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 have access to a dataset that contains latitude and longitude pairs, but they are ill formatted and are not properly represented in the data set.
For example, a lat-long pair might look like this: 31333445, 105530865 when it should be 31.333445, -105.530865. Given the data set I am working with I know that the min value for latitude is 31.0 and the max is 37.0, and the min/max of longitude is -103 to -109.
If I was given a piece of paper and a pencil I could easily correct these myself, but the correction needs to happen on the fly when we receive input from a different program. We have no control over how the data is formatted until it hits our system and then we can make changes and corrections, and the lat-long pairs are all in a integer format listed above rather than a float.
What would be the best way to go about manually correcting this error? I am using PHP for our processing system.
If they're the same length then just divide by 1000000 and make negative where needed:
echo $lat / 1000000;
echo -$lon / 1000000;
If not then get the number of numbers at the start (2 and 3 here) making negative if needed, then insert a decimal and the remaining:
echo substr($lat, 0, 2) . '.' . substr($lat, 2);
echo -substr($lon, 0, 3) . '.' . substr($lon, 3);
You can use floatval() on the results if needed.
If the number of digits is always the same fixed size then use the solution suggested by #AbraCadaver in his comment ( just divide by 1000000 and multiply with -1)
If the number of digits can be different you need a different solution
and I have got a weird idea (at 0:24am)
I would convert the number to a string resulting in "31333445"
then concatenate "0." with the "31333445" resulting in "0.31333445"
then convert it back to a double resulting in 0.31333445
and then multiply it with 100 resulting in 31.333445 (and multiply the other value with -1 )
:-B
With this solution it does not matter if the number you get from outside has 3 or 14 digits
May sound weird but should work.
If this sounds to be a useful solution i will put into code tomorrow.

MySQL mod vs PHP mod [closed]

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
Edited
Sorry guys I want PHP mod to display decimals
In MySQL select mod(15.6,1) result 0.6 but in PHP 15.6 % 1 result 0?
My purpose is to restrict calculation only for exact integer which does not allow decimals
In PHP, the % modulus operator works on integers. The operands 15.6 and 1 are cast to int (15 and 1) before being worked on. Therefore, 15 % 1, as you may expect, gives 0. Therefore "exact integer which does not allow decimals" matches the behaviour in PHP.
In MySQL, as the documentation says:
MOD() also works on values that have a fractional part and returns the exact remainder after division
Therefore, you should TRUNCATE(operand, 0) the operands before using them in MySQL to obtain the integer behaviour. This is consistent with PHP's behaviour of casting to int -- numbers are truncated, not floored (which is a significant difference with negative numbers).

how to find a sequence of 5 number which is the highest [closed]

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 5 years ago.
Improve this question
I have this large string of digits:
$number = "8457714236241996394662789446446900947844174208436229465708413283480252308452309445136821833388596362971481609898423885940334748909310590559808872696932821556902064811261967616862184374009875724727718022667764327386516693129548512719134416243929994231994949382960356875888556457289812078753217535142126801211014214834504421933614164095445014707761460891338932623869783278796726454120129597585667668220935747858847437582800078002228689051590879937597834754848624741161377581359919801031273163364064818325258392890356143251007563549777054047013154878246432673611603091925150515368164497803349723366818641042829792279918398010084882019400234971502609827514132560077285356201267749465601230620222274625799272352699855298156629748948075668465722353974463067725471326703029315330935768523979276352367926340029415999734624584287325909545543547035525815285397244049320738084432091566581396643275969932094091735878708555576059445021204851541525428226299437613379041397828790357002353728103827178553574459018352";
I want to find a sequence of 5 numbers which creates the highest number. I supposed I have to iterate at all numbers then if the sum of numbers is bigger than previous one I have to save it and check it again, but with minus 1 previous and + 1 next. I just have no idea how to put this into script. How can I do it?
First, you need to make a loop. The loop needs to execute a number of times equal to the length of the input string minus the length of the substring you want to evaluate. Calculate that length before constructing the loop.
$digits = 5;
$count = strlen($number) - $digits;
Then initialize a maximum value. Loop from zero to the count you calculated, and take substrings starting from the position indicated by your loop increment variable. Compare them to your previous maximum value and overwrite that value with the current substring if it is greater.
$max = 0;
for ($i = 0; $i <= $count; $i++) {
$substr = substr($number, $i, $digits);
$max = max($max, $substr);
}

How to get a percent value [closed]

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
Okay, so for my website, I made a tests system, where you can take quizes. It all works, but getting the percentage of correct answers is broken.
I have 3 variables I am trying to use to get the percentage.
$incorrect // Int value of incorrect answers
$correct // Int value of correct answers
$perc // Percentage of correct answers from 0 to 100
Currently, I use this, but it does not work:
if($incorrect===0){
$perc = 100;
}elseif($correct===0){
$perc = 0;
}else{
$perc = ($incorrect / $correct)*100;
}
$perc = $correct*100/($incorrect+$correct)
User1950001, as Barmar mentioned, you need to calculate the TOTAL number of answers as a percentage is calculated by (NUMBER OUT OF TOTAL x 100 / TOTAL).
In this case, CORRECT. is your NUMBER OUT OF TOTAL and INCORRECT + CORRECT is your TOTAL, which is what you were missing.
With your original code, perc = ($incorrect/$correct) * 100 where total questions is 25, 20 are incorrect and 5 are correct would return 400% as the value. Obviously this is way off.

check array for limitations [closed]

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 accepting form submissions.
To prevent spoofing of the form I am using php isnumeric to verify that the posted values contain only numeric values.
I would like to check also:
1) that the posted values array contains max 1000 values (because no user would buy more than 1000 items!)
2) that the size of a single array key is composed of max 20 numbers (bigint unsigned max length)
how do I achieve this?
Point 1:
if (count($posted_values) <= 1000)
{
...
}
else
echo "Error";
Point 2:
Did you mean PHP_INT_MAX ?
If yes, just do:
foreach($posted_values as $value)
and then check that $value is less or equal than PHP_INT_MAX.

Categories