php - rand() between the same values - php

I havn't found a solution for this.
I have the numbers 3 and 5.
How do I randomly choose one of those two numbers.
In PHP.
rand() or mt_rand() jsut has min and max parameters.
thank you for your help!

if(mt_rand(0,1)) {
echo 3;
} else {
echo 5;
}
Or reverse it, your choice.

Get a random number with rand() and set $value to it's new value depending on the random number.
$value = rand(0,1) == 0 ? 3 : 5;

Just do a rand between only 2 values then use an if. So:
$randval = rand(0,1);
if($randval == 0)
$value = 3;
else
$value = 5;

You know that you have 2 values so you can do something like this:
$rand = rand(1,2);
if ($rand == 1)
$val = 3;
else
$val = 5;

Related

How can I keep a number in specific range?

I have a function like this:
<?php
function keepInRange($n){
$min = 5;
$max = 15;
if ( $n < $min ) {
$res = $min;
} elseif ( $n > $max ) {
$res = $max;
} else {
$res = $n;
}
return $res;
}
It always returns a number between $min and $max. It works as well, but doesn't seem clean an professional to me. I think it can be better (without those conditions). Any idea how can I make it shorter and cleaner?
If you are trying to make it shorter (and probably cleaner) and also removing those if statements, you can use max() and min() functions:
function keepInRange($n){
$min = 5;
$max = 15;
return max(min($max, $n), $min);
}
Also as #admcfajn mentioned, you can pass $min and $max as arguments to make the function more flexible:
function keepInRange($n, $min = 5, $max = 15){
return max(min($max, $n), $min);
}
Definitely its not the best way, You can use Rand() to generate numbers between a particular range.
Try this
print rand(10, 30) . "";​
It'll generate and print a random number between 10 and 30 (10 and 30 are included).
I think it can be better (without those conditions). Any idea how can I make it shorter and cleaner?
Well if you want it without explicit conditions, then you could simply use min() and max():
function keepInRange2($n) {
$min = 5;
$max = 15;
return max($min, min($max, $n));
}
The order of the functions and parameters might seem odd at first glance - but since you want 15 to be the maximum value, we need to get the minimum of 15 and whatever the value is first - and then vice versa for the minimum 5.
This can be achieved cleaner using ternary operator.
function keepInRange($n){
$min = 5;
$max = 15;
return ( $n < $min ) ? $min : (( $n > $max ) ? $max : $n);
}

How to make php generate random numbers untill the specific number is reached

I want a random integer to be generated in the range from 1 to 3 until 2 will be generated.
Please review the code below - What am I doing wrong?
Thank you!
<?php
$min = 1;
$max = 3;
$number = rand($min,$max);
while($number !== 2) {
echo ($number);
}
?>
your rand() is not in the while loop, so the rand() will execute one time.
If the $number is not 2, the while loop will execute without stoping.
If the $number is 2, the while loop will not executed.
while(($number = rand($min, $max)) != 2){echo $number;}

Round number in php depending on the value

I know the following code will remove the numbers are the point.
round($number);
I want to round the numbers as follows
if number is 20.123
I want result 20,
If number is 20.567
I want result 21
Means if value is below .5 , it should remove that value.
If value if .5 or above it should round up.
How ?
Anyone help ?
round($number) will do what you want:
round(20.156); // 20
round(20.651); // 21
Live example
I am sure it's working well.
<?php
$var = 22.443;
$var = number_format($var, 0, '.', '');
echo $var;
?>
function get_decimal_num($number){
$num = explode('.', $number);
return $num[1];
}
$num = 10.5 ; // for example
$val = get_decimal_num($num);
if($val >= 5)
{
$value = (int) $num;
echo $value = $value + 1;
}
if($val < 5)
{
echo $num;
}
Plese check it...
Try this:
use the ceil, floor, explode and substr function to achieve you value.
$num = "20.123";
$arr = explode(".", $num);
if(substr($arr[1], 0, 1) >= 5){
$num = ceil($num);
}else{
$num = floor($num);
}
echo $num;
Result:
20
Also you can use the round function.
round(20.156); // 20
round(20.651); // 21

if number is a decimal between 2 numbers (php)

I'm writings a PHP script that calculates an average of a number.
EXAMPLE:
$rating = 7;
$votes = 3;
$AvgRating = number_format($rating / $votes,1);
The return $AvgRating for this would be 2.3 (to 1 decimal place).
is it possible to say.....
if ($AvgRating > 2 && $AvgRating < 3){
$display = 'between';
}
echo $display;
I have tried but it does not work, I have tried google but don't know exactly what I need to look for.
This code evaluates correctly...
<?php
$rating = 7;
$votes = 3;
$AvgRating = number_format($rating / $votes,1);
if ($AvgRating > 2 && $AvgRating < 3){
$display = 'between';
}
echo $display;
?>
Hi there is some mistakes $AvgRating retunrs 2.3 not 7.3
So you have to check with this
$rating =7; $votes=3;
$AvgRating = number_format($rating / $votes,1);
if ($AvgRating > 2 && $AvgRating < 3.0){
echo $AvgRating;
}
If your question is whether the condition inside the if statement is correct, yes it is.
However in your case, the if condition fails since the value of $AvgRating (7/3) is less than 7. So you probably might be checking whether $AvgRating is greater than 2.

Choose between two items based upon a given percentage

I need to display one of two items in an array based up on a 40/60% ratio. So, 40% of the time, item one displays and 60% of the time, item two displays.
I now have the following code that will just randomly choose between the two, but need a way to add the percentage weight to it.
$items = array("item1","item2");
$result = array_rand($items, 1);
echo $items[$result];
Any help would be appreciated. Thanks!
Something like that should do the trick
$result = $items[ rand(1, 100) > 40 ? 1 : 0 ];
$val = rand(1,100);
if($val <= 40)
return $items[0];
else
return $items[1];
Just use normal rand method:
if (rand(1,10) <= 4) {
$result = $items[0];
} else {
$result = $items[1];
}
if(rand(0, 100) <= 40) {
# Item one
} else {
# Item two
}
What about ?
$rand = mt_rand(1, 10);
echo (($rand > 4) ? 'item2' : 'item1');
$index = rand(1,10) <= 4 ? 0 : 1;
echo $items[$index];

Categories