I want to create an application that shows multiplication, addition, subtraction and division with 2 random numbers. I made a function that shows random numbers:
function Numbers() {
echo(mt_rand() . "<br>");
echo(mt_rand() . "<br>");
echo(mt_rand(1,10));
}
Numbers();
Can someone explain to me how I can make it go +/- and x each other?
I now changed my code to this:
function Numbers() {
$Number1= echo(mt_rand() . "<br>");
$Number2= echo(mt_rand() . "<br>");
$Number1 + $Number2;
$Number1 - $Number2;
$Number1 / $Number2;
}
Numbers();
Here's an example for the addition, you figure out the rest:
$a = mt_rand();
$b = mt_rand();
echo "$a + $b = " . ($a + $b);
function printRnd()
{
$a_ = rand(1,10);
$b_ = rand(1,10);
echo "a={$a_}, b={$b_}<br><br>";
$plus_ = $a_+$b_;
$minus_ = $a_-$b_;
$multi_ = $a_*$b_;
$divid_ = $a_/$b_;
echo "a+b={$plus_}<br>";
echo "a-b={$minus_}<br>";
echo "a*b={$multi_}<br>";
echo "a/b={$divid_}<br>";
}
printRnd();
Related
I'm working on a website for my friend's gaming clan. I'm trying to have a php code that takes the player's coordinates, loops over some known locations and print the name of the closest location.
I can't get it to work however. It always seems to want to print "Balota" which is index 1 in the array. I also noticed that the cosine function can't be working as all values are above 1.0.
But I'm completely stuck now. I think I've been beating my head over it for too long and its something right in front of me but I can't see it.
<?php
function DotProd($loc1,$loc2){
return array_sum(array_map(create_function('$a, $b', 'return $a * $b;'), $loc1, $loc2));
}
function CosineSim($loc1,$loc2){
return DotProd($loc2,$loc2)/sqrt(DotProd($loc1,$loc2)*DotProd($loc1,$loc2));
}
function Closest($loc){
$novo = array(11300,14300);
$balota = array(4500,2500);
$zelen = array(2500,5200);
$sever = array(7900,12600);
$vybor = array(4500,8200);
$nwaf = array(4500,10200);
$neaf = array(12100,12500);
$kamensk = array(7870,14600);
$bere = array(12900,10000);
$gorka = array(9600,8900);
$elektro = array(10100,2000);
$cherno = array(6600,2600);
$stary = array(6100,7700);
$novy = array(7000,7700);
$mysk = array(1100,7200);
$locations = array($novo,$balota,$zelen,$sever,$vybor,$nwaf,$neaf,
$kamensk,$bere,$gorka,$elektro,$cherno,$stary,$novy,$mysk);
$sim = 99999999999;
$locat = 0;
for($i = 14; $i >= 0; $i--){
$s = CosineSim($loc,$locations[$i]);
echo "<br>" . $i . " " . CosineSim($loc,$locations[$i]);
if($s < $sim){
$sim = $s;
$locat = $i;
}
}
$items = array("Novo","Balota","Zelenogorsk","Severograd","Vybor","NWAF","NEAF","Kamensk Military","Berezino",
"Gorka","Elektro","Cherno","Stary Sobor","Novy Sobor","Myshkino");
return $items[$locat];
}
$x = $_GET["xpos"];
$y = $_GET["ypos"];
$location = array($x,$y);
echo "<b>You are at " . $x . ":" . $y;
$index = Closest($location);
echo "<br>You're pretty close to ". $index . "<br>";
?>
I am using a distance calculation formula based on the link: https://www.mathwarehouse.com/algebra/distance_formula/index.php
I only changed CosineSim. The rest of the code remains the same. And you don't actually need DotProd. It's not elegant but it works for me.
function CosineSim($loc1,$loc2){
// sum of x coordinates
$x1 = ($loc1[0]-$loc2[0])*($loc1[0]-$loc2[0]);
// sum of y coordinates
$y2 = ($loc1[1]-$loc2[1])*($loc1[1]-$loc2[1]);
$summ = $x1 + $y2;
$sqrt_res = sqrt($summ);
return $sqrt_res;
}
If I enter something like:
You are at 4640:7205
...
You're pretty close to Vybor
Hope this helps!
So I am working on my Final Project for a web application development class I'm taking and I am creating a Powerball lottery generator. For this to work, the White ball numbers cannot be duplicated. Here is how my code is looking so far:
<?php
for($x = 1; $x < 6; $x++){
//set each white ball variable (a through e) to a random number between 1 and 69
$a = floor((lcg_value() * 69 + 1));
$b = floor((lcg_value() * 69 + 1));
$c = floor((lcg_value() * 69 + 1));
$d = floor((lcg_value() * 69 + 1));
$e = floor((lcg_value() * 69 + 1));
//set powerball number variable to a number between 1 and 26
$f = floor((lcg_value() * 26 + 1));
//echo all white ball numbers and powerball number
echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
};
?>
The issue with this code is that there is a chance that variables 'a' through 'e' have a chance of being duplicate numbers. What code could I use to ensure that none of the variables 'a' through 'e' are the same? I thought of doing something like:
if($a != $b || $a != $c || $a || $d...){
//echo numbers
}else{
//generate new numbers
};
But that is just too much work and I always try to find the most efficient ways to write code. I don't want to have to write more code than I need to. Any assistance would be greatly appreciated. Thank you in advance!
You could generate the numbers this way:
$arr = range(1, 69);
shuffle($arr);
$a = $arr[0];
$b = $arr[1];
$c = $arr[2];
$d = $arr[3];
$e = $arr[4];
Also take a look at Generating random numbers without repeats
Add them in an array and check for uniqueness:
<?php
for($x = 1; $x < 6; $x++){
$unique = false;
while(!$unique) {
//set each white ball variable (a through e) to a random number between 1 and 69
$a = floor((lcg_value() * 69 + 1));
$b = floor((lcg_value() * 69 + 1));
$c = floor((lcg_value() * 69 + 1));
$d = floor((lcg_value() * 69 + 1));
$e = floor((lcg_value() * 69 + 1));
$numbers = array($a, $b, $c, $d, $e);
if(count($numbers) == count(array_unique($numbers)) {
$unique = true;
}
}
//set powerball number variable to a number between 1 and 26
$f = floor((lcg_value() * 26 + 1));
//echo all white ball numbers and powerball number
echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
}
While loop the random generation of numbers and check for duplicates on the fly.
Test it here:
https://3v4l.org/odOqb
I have changed the random numbers to a smaller size to see if it does create duplicates.
But I have not seen any.
<?php
$arr =array();
for($x = 1; $x < 6; $x++){
//set each white ball variable (a through e) to a random number between 1 and 69
While (count($arr) != 5){
$arr[] = floor((lcg_value() * 6 + 1));
$arr = array_unique($arr);
}
//set powerball number variable to a number between 1 and 26
$f = floor((lcg_value() * 26 + 1));
Var_dump($arr);
//echo all white ball numbers and powerball number
//echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
};
To make it as efficient as possible you do not want to have to generate a new set of numbers each time therefore if a duplicate appears you would just want to re-pick for that letter right away.
To do this you can add the elements to an array and search through it after each letter to make sure its a unique number. This is done through the utilization of the for loop, while loop, and check variable.
<?php
for($x = 1; $x < 6; $x++) {
//set each white ball variable (a through e) to a random number between 1 and 69
$uniqueNumbers = array();
$check = true;
$a = floor((lcg_value() * 69 + 1));
array_push($uniqueNumbers, $a);
while ($check) {
$check = false;
$b = floor((lcg_value() * 69 + 1));
foreach ($uniqueNumbers as $element) {
if ($b == $element) {
$check = true;
}
}
}
array_push($uniqueNumbers, $b);
$check = true;
while ($check) {
$check = false;
$c = floor((lcg_value() * 69 + 1));
foreach ($uniqueNumbers as $element) {
if ($c == $element) {
$check = true;
}
}
}
array_push($uniqueNumbers, $c);
$check = true;
while ($check) {
$check = false;
$d = floor((lcg_value() * 69 + 1));
foreach ($uniqueNumbers as $element) {
if ($d == $element) {
$check = true;
}
}
}
array_push($uniqueNumbers, $d);
$check = true;
while ($check) {
$check = false;
$e = floor((lcg_value() * 69 + 1));
foreach ($uniqueNumbers as $element) {
if ($e == $element) {
$check = true;
}
}
}
array_push($uniqueNumbers, $e);
//set powerball number variable to a number between 1 and 26
$f = floor((lcg_value() * 26 + 1));
//echo all white ball numbers and powerball number
echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
}
Below code is for 6/49 Canada lottery
<body bgcolor="gold"><font size="9"></font>
<pre>
<?php
// Code by bhupinder Deol . modify according to needs
for ($i=0;$i<=10;$i++) {
for ($x=0;$x<=5;$x++) {
$rand[$x]=rand(1,49);
}
asort($rand);
$result = array_unique($rand);
$count = count($result);
if ($count == 6) {
print_r($rand);
$x=0;
}
else
{
echo "same numbers in array";
--$x;
}
}
?>
</pre>
</body>
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
Can some one tell why my php line break not working ( echoing ) ?
I know i can write the code in a different way to make the line break work, but i want to know the reason behind this ?
<?php
$var1 = 3;
echo "Addition = " . $var1 += 3 . "<br>";
echo "Subtraction = " . $var1 -= 3 . "<br>";
echo "Multiplication = " . $var1 *= 3 . "<br>";
echo "Division = " . $var1 /= 3 . "<br>";
?>
Well seems like I have to clean some things up here.
Let's take a look at the operator precedence, which says:
. has a higher precedence, than +=, -=, *=, /=
. is left associative
=, +=, -=, *=, /= is right associative
We also take a look at the note at the bottom of the manual:
Note:
Although = has a lower precedence than most other operators, PHP will still allow expressions similar to the following: if (!$a = foo()), in which case the return value of foo() is put into $a.
Means that even tough = has a lower precedence than . it gets evaluated first. You can also see this if you do something like this:
$xy = "HERE";
echo "I am " . $xy = "NOT HERE";
Now you would think that . has a higher precedence than = and will get evaluated first, but as from the note in the manual, the assignment is first and you end up with this:
echo "I am " . ($xy = "NOT HERE");
output:
I am NOT HERE
So if we put all these information's together, we can say, that the assignment gets evaluated first, but it's right assocative. Means this:
$var1 = 3;
echo "Addition = " . ($var1 += 3 . "<br>");
echo "Subtraction = " . ($var1 -= 3 . "<br>");
echo "Addition = " . ($var1 *= 3 . "<br>");
echo "Addition = " . ($var1 /= 3 . "<br>");
So this code will end up in this:
echo "Addition = " . ($var1 += "3<br>");
echo "Subtraction = " . ($var1 -= "3<br>");
echo "Addition = " . ($var1 *= "3<br>");
echo "Addition = " . ($var1 /= "3<br>");
Which then through the arithmetic operator gets convert to an integer we end up with this:
echo "Addition = " . ($var1 += 3);
echo "Subtraction = " . ($var1 -= 3);
echo "Addition = " . ($var1 *= 3);
echo "Addition = " . ($var1 /= 3);
And after the assignment is done the concatenation gets evaluated, which looks like this:
echo "Addition = " . 6;
echo "Subtraction = " . 3;
echo "Addition = " . 9;
echo "Addition = " . 3;
With this you end up in this output:
Addition = 6Subtraction = 3Addition = 9Addition = 3
And now how to solve this? Simply wrap your assignment in parentheses, so that the <br> tag doesn't get into the assignment. E.g.
echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) . "<br>";
echo "Multiplication = " . ($var1 *= 3) . "<br>";
echo "Division = " . ($var1 /= 3) . "<br>";
//^ ^ So the br tag doesn't get in the assignment of the variable.
This is happening because of the type casting issues. 3 . "<br>" will be converted to number while the operation will be performed. Wrap the inside () so that the operations are performed first then the concatenation.
echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) ."<br>";
echo "Addition = " . ($var1 *= 3) . "<br>";
echo "Addition = " . ($var1 /= 3) ."<br>";
You can use commas,
echo "Addition = " . $var1 += 3 , "<br>";
echo "Subtraction = " . $var1 -= 3 ,"<br>";
echo "Addition = " . $var1 *= 3 , "<br>";
echo "Addition = " . $var1 /= 3 ,"<br>";
Or wrap it in brackets:
echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) ."<br>";
echo "Addition = " . ($var1 *= 3) . "<br>";
echo "Addition = " . ($var1 /= 3) ."<br>";
Otherwise the 3 number is concatenated with <br>.
Your PHP means:
echo "Addition = " . $var1 += (3 . "<br>");
echo "Subtraction = " . $var1 -= (3 ."<br>");
echo "Addition = " . $var1 *= (3 . "<br>");
echo "Addition = " . $var1 /= (3 ."<br>");
And number + 3 . '<br>' is number + (int)(3 . '<br>') which is number + 3. No <br> exists now due to retyping to number(converting to number).
Use brackets around equations.
echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) ."<br>";
echo "Addition = " . ($var1 *= 3) . "<br>";
echo "Addition = " . ($var1 /= 3) ."<br>";
Try this..
"." is used for php variable to concate not for numbers
<?php
$var1 = 3;
echo "Addition = ". ($var1 += 3) ."</br>";
echo "Subtraction = ". ($var1 -= 3) ."</br>";
echo "Addition = ". ($var1 *= 3) ."</br>";
echo "Addition = ". ($var1 /= 3) ."</br>";
?>
Try this way.
<?php
$var1 = 3;
echo "Addition =" . ($var1 += 3 ).'<br>';
echo "Subtraction =" . ($var1 -= 3).'<br>';
echo "Addition =" . ($var1 *= 3 ).'<br>';
echo "Addition =" . ($var1 /= 3 ).'<br>';
?>
I have written a small program to solve a mathematical problem. But when I run, it gives an undefined offset error on line number 9,11,13,15.
I have searched various questions, but didn't find anything useful.
What might be causing this. ?
<?php
$arr = [1,3,5,7,9,11,13,15];
$tries=0;
$answer=0;
while(($answer!=30) && ($tries!=1000))
{
$tries = $tries+1;
$num1=getRandomNumber();
$num2=getRandomNumber();
$num3=getRandomNumber();
$num4=getRandomNumber();
$num5=getRandomNumber();
if($num5 + $num4 + $num3 + $num2 + $num1 == 30)
{
$answer = 30;
echo $num1 + "+" + $num2 + "+" + $num3 + "+" + $num4 + "+" + $num5 + " = 30";
break;
}
}
if($tries==1000)
{
echo "1000 tries completed";
}
function getRandomNumber()
{
$arr = [1,3,5,7,9,11,13,15];
$r = mt_rand(1,15);
if(($r%2)!=0)
{
return $arr[$r];
}
}
?>
In your getRandomNumber() function, you're generating an array index between 1 and 15, but your array is only 8 elements long.
To fix this, update the call to mt_rand() to support your actual array size:
$r = mt_rand(0, count($arr) - 1);
Side-note (not answer specific), string concatenation in PHP is done with the period, . and not the +:
echo $num1 + "+" + $num2 + "+" + $num3 + "+" + $num4 + "+" + $num5 + " = 30";
// should be:
echo $num1 . "+" . $num2 . "+" . $num3 . "+" . $num4 . "+" . $num5 . " = 30";
You should change line:
$r = mt_rand(1,15);
into
$r = mt_rand(0,count($arr)-1);
because your $arr in your getRandomNumber function has only 8 elements (not 16)
function getRandomNumber()
{
$arr = [1,3,5,7,9,11,13,15];
$r = mt_rand(1,15);
if(($r%2)!=0)
{
return $arr[$r];
}
}
The mt_rand function returns a number higher then the array index witch is 7. You can either extend the array and make it have 16 index or reduce the range in mt_rand function to 0-7.
if($koltukk%4 == 2){
if($koltukk > 1000){
$koltukH = "B";
(int)$koltukR = ($koltukk / 4) + 1;//Doesnt work (int)
}
else{
$koltukH = "E";
(int)$koltukR = ($koltukk / 4) + 1;//Doesnt work (int)
}
}
$koltukR = ($koltukk / 4) + 1;
I want to get the $koltukR variable as an integer but i couldn't do it (int) did not work
You need to use the (int) casting on the other side of the assignment operator:
$koltukR = (int)(($koltukk / 4) + 1);
Or, use intval() like this:
$kolturR = intval(($koltukk / 4) + 1);
$koltukR = intval(($koltukk / 4) + 1);
You should use better variable names, move the math out of the if/else since both are the same, and you shouldn't even need to cast this as an int manually.
PHP has an intval() method that turns a variable into an integer. Pass in your variable as a parameter.
intval()
<?php
if($koltukk%4 == 2)
{
if($koltukk > 1000)
{
$koltukH = "B";
$koltukR = (int)(($koltukk / 4) + 1);
} else{
$koltukH = "E";
$koltukR = (int)(($koltukk / 4) + 1);
}
}
echo $koltukR;
?>
One important note here: intval() is NOT round(). intval() is similar to floor().
I think what you really want is round():
Here's the real answer: K.I.S.S.
if($k%4 == 2){
if($k > 1000){
$H = "B"
}
else{
$H = "E";
}
}
$R = round($k / 4) + 1;
Stealing an example from http://us2.php.net/intval to illustrate:
echo number_format(8.20*100, 20), "<br />";
echo intval(8.20*100), "<br />";
echo floor(8.20*100), "<br />";
echo round(8.20*100), "<br />";
819.99999999999988631316
819
819
820