Page 1 of 1

if in for?

Posted: Thu Jun 17, 2010 11:43 am
by Termin8r
Is this kind of code possible?

Code: Select all

<?php
$k = rand(1,10);
$A = 0; 
for ($i = 1; $i<=1000000;$i++) 
{
   if ($k==1)
      $A= $A + 1;
}
echo $Ap; 

?> 
I want to be able to create a set of random values, and increment one specific variable if the random number equals, 1, 2, 3... or some othe value I chose.
How should I program this? What am I doing wrong?

Thanks

Re: if in for?

Posted: Thu Jun 17, 2010 11:52 am
by Kurby
Your code basically does this.

Code: Select all

<?php
$k = rand(1,10);
$A = 0; 
if($k == 1)
{
    $A = 1000000;
}
echo $A;

?>
If $k is a certain number set $A to a big number. I'm a little unclear as to what you want to do, perhaps you can explain it more?

Re: if in for?

Posted: Thu Jun 17, 2010 12:11 pm
by Termin8r
What I want to do is simulate a random election, by creating av random variable k. If this contains a certain number (eg 1) I want party A to get one more wote and. If this contains for example the value 3, I want party D to get one more vote, and so on...

Re: if in for?

Posted: Thu Jun 17, 2010 12:16 pm
by Kurby

Code: Select all

$numofvoters = 10000
for ($i = 1; $i <= $numofvoters; $i++) 
{
    $k = rand(0, 2);
    switch ($k) {
    case 0:
        $A++;
        break;
    case 1:
        $B++;
        break;
    case 2:
        $C++;
        break;
    }
}

Re: if in for?

Posted: Thu Jun 17, 2010 12:22 pm
by hypedupdawg
I've made a few changes below to get it to work:
1) You need to set the "add one" expression inside curly brackets; this makes it part of the if() function.
2) You were echoing the variable $Ap for some reason; you just need to echo the variable $A.

Code: Select all

<?php
$k = rand(1,10);
$A = 0;
for ($i = 1; $i<=1000000;$i++)
{
   if ($k==1)
      {
      $A= $A + 1; // This you did not set inside parentheses (curly brackets)
      }
}
echo $A; // This you set as $Ap for some reason

?>
Hope this helps!

Re: if in for?

Posted: Thu Jun 17, 2010 12:56 pm
by AbraCadaver
For fun with a resulting array for ease of use:

Code: Select all

$parties = range('A', 'D');

for($i=1; $i<=1000000; $i++) {
	$key = array_rand($parties);
	
	if(!isset($votes[$parties[$key]])) {
		$votes[$parties[$key]] = 0;
	}
	$votes[$parties[$key]]++;
}
print_r($votes);

Re: if in for?

Posted: Fri Jun 18, 2010 9:57 am
by Termin8r
To hypedupdawg:

Thanks, but the code only prints 0 Which is the value set for A before the foor loop. Why?

Thanks

Re: if in for?

Posted: Fri Jun 18, 2010 10:05 am
by AbraCadaver
Termin8r wrote:To hypedupdawg:

Thanks, but the code only prints 0 Which is the value set for A before the foor loop. Why?

Thanks
Because the rand() only runs once before the loop so $A will always be either 0 if the rand() is not 1 or 1000000 if rand() returned 1. Move the rand() inside the loop, but you'll probably have better luck with the code I posted.

Re: if in for?

Posted: Fri Jun 18, 2010 2:14 pm
by Kurby
AbraCadaver wrote:
Termin8r wrote:To hypedupdawg:

Thanks, but the code only prints 0 Which is the value set for A before the foor loop. Why?

Thanks
Because the rand() only runs once before the loop so $A will always be either 0 if the rand() is not 1 or 1000000 if rand() returned 1. Move the rand() inside the loop, but you'll probably have better luck with the code I posted.
As I did with mine :)

Re: if in for?

Posted: Sun Jun 20, 2010 9:51 am
by Termin8r
Ah! It worked! Thank you very much!:)

I have a follow up:

If I've got an array like

array(1=>Horse,3=>Cat,5=>Dog,.......)

How do I make a code that outputs

Animal Age
Horse 1
Cat 3
Dog 5
..........
..........

Re: if in for?

Posted: Sun Jun 20, 2010 11:10 am
by AbraCadaver

Code: Select all

foreach()

Re: if in for?

Posted: Sun Jun 20, 2010 12:42 pm
by Termin8r
Ok, I've tried this
<html>
<body>
<table border="1">
<tr><th>Animal</th><th>Age</th></tr>
<tr></tr>
<?php
$animalage = array_combine($animal,$age);
var_dump($resultat);
foreach ($animalage as $key => $value)
{
echo "<td>" $key "</td>";
echo "<td>" $value "</td>";
echo ""break tag"";
}
?>
</table>

I know that the foreach loop works, but when I tried making a table of it, it stopped working. Where is my mistake?
Break tag: the br tag....

Re: if in for?

Posted: Sun Jun 20, 2010 4:02 pm
by AbraCadaver
At the start of the loop (inside) you need a <tr> and at the end (inside) you need a </tr> so that each iteration is a new row.