First, I'll talk something about my problem here, then I will give some of the source code to get a better insight into the problem.
I use randoms to randomly decide who will be fighting who(random battles)There are 13 types of units, so it is x=rand(1,13) to ensure x is a natural number n, example: 5 or 8 I made round(rand(1,13)) now the problem is as following, I got this rand and twelve other rands(13 in total) which should all give a number between 1 and 13. Now to avoid conflict, every one of these numbers should be different, so random #1 could have number 1, but random #8 can't have 1 as well. The problem is, that it does give the same numbers again, despite my effort to avoid that.
Here's a bit of source code:
while($count1 != 1){ $rrand = round(rand(1,13)); echo "1: $rrand<BR>"; $count1 = 1; }Now this part defines the initial value of the rand(), of course, this is just one of the 13, but there's no real need to show them all, as they are all practically the same. The echo is a test to see what value it gets. $count1 is used to go back to this loop later on in the script, in certain situations as in, you get the value of the rand, you go into a switch with 13 situations(1-13). If the rand returns 1, then situation 1 is going to happen, but if the other user does not have that type of unit, so unit>=1, then we continue the script, else we set $count1 to 0, so the loop starts again, and the rand() generates a new number.
Then we got the first check to see if the output of a rand is the same as another.
if($krand == $orand) { $count13 = 0; } elseif($krand == $brand) { $count13 = 0; } elseif($krand == $lrand) { $count13 = 0; } elseif($krand == $jrand) { $count13 = 0; } elseif($krand == $crand) { $count13 = 0; } elseif($krand == $frand) { $count13 = 0; } elseif($krand == $vrand) { $count13 = 0; } elseif($krand == $mrand) { $count13 = 0; } elseif($krand == $trand) { $count13 = 0; } elseif($krand == $srand) { $count13 = 0; } elseif($krand == $arand) { $count13 = 0; } elseif($krand == $rrand) { $count13 = 0; }Pretty self-explanatory, if $krand is the same as another rand($arand , $srand, etc) then we go back to his own loop als output a new number, until it got a number that the other rand's didn't get yet.(I chose this one, since this one is the last rand that is called, and thus should get the last remaining number, that's why it checks with every other rand if it's the same or not).
Then for the actual fighting here, let's see how the rand is used in here:
switch($rrand){// What value did we get from the rand() ? case 1:// In case the rand() returned 1 if($amountRiflemanb >= 1){//In case the opponent has that type of unit switch($role){ // What role do you have? case "attacker": // In case you are the attacker $sra = $sra * 1.05; $srb = $srb * 0.95;// This is for balancing, sra= damage that the Rifleman deals(defined earlier on) break; case "defender":// In case you are the defender $sra = $sra * 0.95; $srb = $srb * 1.05; break; } $verliesra = round($srb / $RiflemanHPA);// The loss of your Rifleman = round(damage dealt by opponent's Riflemen / HP of your Rifleman) $verliesrb = round($sra / $RiflemanHPB); } else{// In case your opponent does not have Riflemen, then we go back to the loop to generate a new number $count1 = 0; } break;// Rifleman vs Rifleman
So I think here I would have been protected from having the same values for the rands, turns out that I'm wrong. It does produce the same numbers.
Output(for the rands) are the following(before it goes into any kind of checking):(First # of rand, then the output)
1: 11
2: 2
3: 6
4: 12
5: 2
6: 1
7: 6
8: 3
9: 4
10: 3
11: 11
12: 11
13: 2
As you can see, it produces numbers that are the same, but these are what we start with.
At the end of the script, I have placed another echo of these values to see how they changed, and this is the result:
1: 11
2: 2
3: 6
4: 12
5: 2
6: 1
7: 6
8: 3
9: 4
10: 3
11: 11
12: 11
13: 2
They are the same. Bummer.
So yeah, I'm completely out of ideas.
If anyone has suggestions or ideas, or possibly even solutions, I would be a very happy man.
Thank you in advance.