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 7 years ago.
Improve this question
I had a list of numbers between 1 until 100000000
I need a function to get a number and return a random number between 1 to 30 For each other number...But unique!
for example each time I call getrandomnumber('3') it always return me 25! (no other random number between 1 ...30 like rand(1,30)
You could use the given number as a seed and generate the number using the standard random number function.
function getrand($num)
{
srand($num);
return rand(1 , 30);
}
The Answer
for($i = 0;$i<100;$i++)
echo '<br>'.getrand($i);
function getrand($num)
{
$num = substr($num, -2);
$num = (int)$num;
if($num < 31) return $num;
elseif($num > 30 && $num < 61) return $num - 30;
elseif($num > 60 && $num < 91) return $num - 60;
elseif($num > 90 && $num < 101) return $num - 70;
}
1-30 repeat:
$x=0;
for($i = 0;$i<100;$i++){
$x++;
if($x>30)$x=1;
echo '<br>'.$x;
}
Related
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 want to have an php check for easy passwords patterns like
123456
123456789
abcde
98764321
101010
202020
Is it possible to have a check like this that doesn't rely in maintaining an array of pre-defined strings ?
Just iterate to each letter and check if the next one is same, one less or one more as current. So you can create a score of uniqueness.
For last case you can check if there are duplicate characters and remove from score, too.
$passwords = ['123456', '123456789', 'abcde', '98764321', '101010', '202020', 'xhdgszu'];
foreach ($passwords as $password) {
$score = $length = strlen($password);
$chars = str_split($password);
for ($i = 0; $i < $length - 1; $i++) {
$currentChar = $chars[$i];
$nextChar = $chars[$i + 1];
if ($currentChar === $nextChar
|| ord($currentChar) - 1 === ord($nextChar)
|| ord($currentChar) + 1 === ord($nextChar)
) {
$score--;
continue;
}
}
$unique = array_unique($chars);
$score -= $length - count($unique);
echo "{$password}: {$score} / {$length} ", ($score < $length) ? 'Bad' : 'Good', PHP_EOL;
}
123456: 1 / 6 Bad
123456789: 1 / 9 Bad
abcde: 1 / 5 Bad
98764321: 2 / 8 Bad
101010: -3 / 6 Bad
202020: 2 / 6 Bad
xhdgszu: 7 / 7 Good
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a random function between 1 and 100
I want it to random out 2 < x <10, x is the number I need, if x does not randomize the number in that range then continue random until satisfactory, how to write
Thank.
$x = rand(1,100);
if (2 < $x < 100){echo 'ok'; }
$x = rand(1,100);
if (2 < $x < 100){echo 'ok'; }
Just use a simple while loop:
$x = rand(1,100);
while (2 > $x || $x > 10) {
$x = rand(1,100);
}
echo $x;
The above loop will only stop if your number is between your specified range (not sure if I understood your needs correctly).
Another approach could be to simply use a different interval for the rand() function.
EDIT: Please check the comments of your question, there is a solution.
<?php
for (
$i = 0, $limit = 10;
$i < $limit && $x = rand(1,100);
$i++
) {
echo $x, ' ', (2 < $x && $x < 10) ? 'HIT' : 'MISS', "\n";
}
Example output:
76 MISS
83 MISS
59 MISS
13 MISS
7 HIT
18 MISS
9 HIT
39 MISS
79 MISS
91 MISS
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 7 years ago.
Improve this question
I want to get a percentage of the current value between -60 and 60.
so if
NUMBER == 60
The result should be 100%
NUMBER == -60
The result should be 0%
NUMBER == 0
The result should be 50%
How i can achieve this?
This is basic math
MIN = -60;
MAX = 60;
if (NUMBER < MIN) {
PERCENT = 0;
}
else if (NUMBER > MAX) {
PERCENT = 100;
}
else {
PERCENT = (NUMBER-MIN)*100/(MAX-MIN);
}
(NUMBER + 60) / 1.2
Or
(NUMBER + 60) / 120 / 100
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
Lets say we have 1300 units. now i want to first 500 units to be a price of 12 euro and the other 800 a price of 9 euro.
So what i basicly need is a function that calculates over a total number and gets the first 500 * 12 euro and the other 800 * 9 euro. But the total unit can also be 5000 instead of this example of 1300 unit, its user input ofcouse.
<?php
$total = 1300;
$sub = 1;
$sub2 = 1;
for($i=1; $i<=$total; $i++)
{
for($sub; $i<=500; $sub++)
{
$sub2 += $sub;
break;
}
}
echo $sub2;
?>
<?php
$units = 1300;
$cut_off=500;//made it a var just in case
$a=12;//fist 500
$b=9;//rest
//verbose calculations, you don't really need to create the extra variables
$a_total=$cut_off*$a;
$b_total=($units-$cut_off)*$b;
$grand_total=$a_total+$b_total;
?>
you should probably add a check if the units imputed is less than 500. or things will turn out weird
Updated:
<?php
function calc_total($qtt, $min = 500, $fst_price = 12, $nxt_price = 9){
if($qtt > $min) return false; // Check if the minimum 500 was sent.
/* First calculate the rest, which is the quantity sent by
the user without the minimum x 9. Then add the minimum x 12 */
$total = (($qtt - $min) * 9) + ($min * 12);
return $total;
}
?>
You can call the function like this:
echo "$". calc_total(1300);
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 9 years ago.
Improve this question
I am trying to create a php code to execute a list of divisions, i have tried to put a code together but being a novice, it's not quite working.
<?php
$division(6,true);
for ($i = 1; $i <= 6; $i++) {
if(($division / $i) == $result)
{echo "<p>$division &division; $i = $result</p>";}
}
?>
$division is meant to be the main number, $division will get divided by every number upto and including $division.
$i should list a string of number to from 1 to 6 in this case.
then $divide ÷ $i = $result.
I am hoping for this to print out the list as shown below.
6 ÷ 1 = 6
6 ÷ 2 = 3
6 ÷ 3 = 2
6 ÷ 4 = 1.5
6 ÷ 5 = 1.2
try this...? I'm not sure what the IF statement is trying to accomplish, though
<?php
division(6);
function division($num)
{
for ($i = 1; $i < $num; $i++)
{
$result = $num / $i;
//if(($division / $i) == $result)
//{
echo "$num / $i = $result<br/>";
//}
}
}
?>
output:
6 / 1 = 6
6 / 2 = 3
6 / 3 = 2
6 / 4 = 1.5
6 / 5 = 1.2
edit: please note that % is usually the modulus operator in programming.