So this make sense in my head.. But I'm lost, I know it's going to be something simple..
Hoping someone out there knows what's going on lol..

What I'm trying to do, is get a random number each time from 0 to 5.
Check the result of each random number.
Then get it to tell me how many times that random number came up.

But what is happening is the results I'm getting back are totally wrong.. I'm looping 10 times as expected. But the incrementing seems wrong.. $n5 always comes out at 10, the last time I ran this, the number 5 only came out twice. So how can that be 10?

It's going to be a school boy error I know, But I'm seriously lacking caffeine here.

Here is the code.

Pastebin

11 years ago*

Comment has been collapsed.

I have no knowledge in php specifically, but in some languages, when using random numbers, you have to "randomize" the random numbers, otherwise the number is generated always in the same way.

It's a random (not intended) thing that came to me, I don't know if it's your case. Good luck! :)

11 years ago
Permalink

Comment has been collapsed.

You need breaks in your switch cases.

11 years ago
Permalink

Comment has been collapsed.

Also I would think about putting your results into an array.

11 years ago
Permalink

Comment has been collapsed.

Yeah I'm going to put them into an array as the numbers are going to go right up, but I just wanted to get the logic right first :)

Thanks anyway buddy

11 years ago
Permalink

Comment has been collapsed.

<?php
$stack = array();
for($i=0; $i<10; $i++) $stack[rand(0,5)]++;
for($i=0; $i<=5; $i++) echo @$stack[$i].' times '.$i.'<br/>';
?>

11 years ago
Permalink

Comment has been collapsed.

Urgh, my eyes. You can use more than one line here.

11 years ago
Permalink

Comment has been collapsed.

didnt work for some reason

11 years ago
Permalink

Comment has been collapsed.

It's because of Markdown. You can either use two line breaks or put two space characters after each line.

Two line

break example

Single line break
with spaces

11 years ago
Permalink

Comment has been collapsed.

I have cero PHP experience, but doesn't PHP's switch use breaks on the cases??

11 years ago
Permalink

Comment has been collapsed.

You forgot the break operator in your switch construction. Try to read this http://www.php.net/manual/en/control-structures.switch.php

11 years ago
Permalink

Comment has been collapsed.

Call break after each switch case.

11 years ago
Permalink

Comment has been collapsed.

BAM!

Knew it was a schoolboy error, thanks so much.

11 years ago
Permalink

Comment has been collapsed.

You're using switch-case wrong.

switch ($roll){
case 0 : $n0++; break;
case 1 : $n1++; break;
case 2 : $n2++; break;
case 3 : $n3++; break;
case 4 : $n4++; break;
case 5 : $n5++; break;

}
should do a trick. :)

11 years ago
Permalink

Comment has been collapsed.

Closed 11 years ago by slugshead.