Jump to content

Random without duplicates...


Jay2391

Recommended Posts

I am trying to store randon numbers in variable without haven't any duplicates

I did this ... I just want to know if there is a easier way because i what to know what
to do when i get to 32 teams...I mean this works just a lot of code

::)

[code]<?php

$teams = 4;
if ($teams == 4){
$random1 = rand (1,$teams);
$random2 = rand (1,$teams);   
$random3 = rand (1,$teams);
$random4 = rand (1,$teams);

while($random1 == $random2 or $random1 == $random3 or $random1 == $random4
      or $random2 == $random3 or $random2 == $random4 or $random3 == $random4){

        $random2 = rand (1,$teams);   
        $random3 = rand (1,$teams);
        $random4 = rand (1,$teams);  
  }

echo "$random1<br>";
echo "$random2<br>";
echo "$random3<br>";
echo "$random4<br>";
}



?>[/code]
Link to comment
Share on other sites

i'm not sure what you're trying to do here. maybe this is what you want:
[code]
<?php
$teams = 4;
$input = rand(1, $teams);
$rand_array = array();

for($i = 0; $i < $teams; $i++){
foreach($rand_array as $key => $val){
(($input != $val) ? ($bool = false) : ($bool = true));
}
(($bool == false) ? ($rand_array[] = $input));
}

print_r($rand_array);
?>
[/code]
Link to comment
Share on other sites

[quote author=ninja link=topic=124729.msg517235#msg517235 date=1170180101]
so..are you just wanting like, the numbers 1 through 32 in random order?

[code]
for ($num = 1; $num < 33; $num++) {
  $number[] = $num;
}
shuffle($number);
[/code]
[/quote]

i think it may have something to do with the value of $teams. at first i thought exactly what you thought. but then i saw that $teams = 4. and then he has four different $random variables... i figured maybe they're related. not sure tho.
Link to comment
Share on other sites

well the $number array I made is a well..it's an array. It's like having 42 unique variables, only since it's an array, it's easy to do things with those 42 unique variables, like loop through them, shuffle them around, not have to write out 42 unique variables, etc..  You can't seriously be wanting to write out 42 individual variables, are you? Especially if they are all pretty much going to represent the same thing, only different "teams" or whatever. 
Link to comment
Share on other sites

hey guys I was trying a few of this ideas but they didn't work anyone else know what to do ???

and make it simple i have a pres et number X belive is 4

I need 1 thru 4 randon and no duplicates in a variable.. I did it on top but it get complicated when that number changes

Link to comment
Share on other sites

This question is very similar to another that was posted just a few days ago. Here's the modification of my answer:
[code]<?php
$ok_rand = array();
$max = 4;
for ($i=0;$i<$max;$i++) {
    $test_rand = rand(1,$max);  // get a random number
    while(in_array($test_rand,$ok_rand)) $test_rand =  rand(1,$max);  // have we seen this number already? Yes -- generate another
    $ok_rand[$i] = $test_rand; // store unique number
}
$unq = array_unique($ok_rand);
$dif = array_diff($ok_rand,$unq);
if (empty($dif)) echo "All generated random numbers are unique";
echo '<pre>' . print_r($ok_rand,true) . '</pre>';
?>[/code]

Just change $max to get more numbers.

Ken
Link to comment
Share on other sites

I get this ...

All generated random numbers are unique
Array
(
    [0] => 1
    [1] => 4
    [2] => 2
    [3] => 3
)

which is great but i need to attach a name to that number so i need it to print like
1 then i attach the name
2 attach another name ...

how do i print that
Link to comment
Share on other sites


Thanks got it

<?php
$ok_rand = array();
$max = 10;
for ($i=0;$i<$max;$i++) {
    $test_rand = rand(1,$max);  // get a random number
    while(in_array($test_rand,$ok_rand)) $test_rand =  rand(1,$max);  // have we seen this number already? Yes -- generate another
    $ok_rand[$i] = $test_rand; // store unique number

echo "$test_rand<br>";
}



?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.