2017-05-30 7 views
0

私は、私が取っているWebアプリケーション開発クラスの最終プロジェクトに取り組んでおり、Powerball抽選機を作成しています。これを行うには、白のボール番号を複製することはできません。ここに私のコードは、これまで見ている方法です。変数が等しくないことを確認してください(宝くじコード)

<?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 />"; 
}; 
?> 

このコードの問題は、「」「E」を通じて変数が重複した数字であることのチャンスを持っている可能性があるということです。変数 'a'〜 'e'のどれも同じでないことを保証するために、どのようなコードを使用できますか?

if($a != $b || $a != $c || $a || $d...){ 
//echo numbers 
}else{ 
//generate new numbers 
}; 

しかし、それはあまりにも多くの作業であり、私は常にコードを書く最も効率的な方法を見つけようとしています。私は必要以上にコードを書く必要はありません。どんな支援も大歓迎です。前もって感謝します!

答えて

2

あなたは数字をこのように生成することができます:ループランダムながら

$arr = range(1, 69); 
shuffle($arr); 
$a = $arr[0]; 
$b = $arr[1]; 
$c = $arr[2]; 
$d = $arr[3]; 
$e = $arr[4]; 

はまたGenerating random numbers without repeats

+0

非常にシンプルです。いいね! – Andreas

0

配列にそれらを追加して、一意性をチェック:

<?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 />"; 
    } 
0

を見てみましょう数字の生成とその場での重複のチェックを行います。ここ

テストは: https://3v4l.org/odOqb
私はそれが複製を作成ないかどうかを確認するために小さいサイズに乱数を変更しました。
しかし、私は見たことがありません。

<?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 />"; 
}; 
0

あなたが重複してあなただけすぐにその手紙のためのピックを再したい表示された場合ので、数字の新しいセットを毎回生成する必要がありますする必要はありません可能な限り効率的にするために。

これを行うには、要素を配列に追加し、各文字の後にその要素を検索して一意の番号を確認します。これは、forループ、whileループ、およびcheck変数の使用によって行われます。コードの下

<?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 />"; 
} 
+0

ただのヒント。何度も同じコードを繰り返すと、そのコードを機能させることができます。より洗練されたコード。 – Andreas

+0

良い点、私はそれについても考えなかった – bgibso4

0

6/49カナダ宝くじ

<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> 
+3

ようこそスタックオーバーフロー!スタンドアロンコードスニペットは自己説明的ではなく、有用ではない可能性があるため、回答を説明してください。ありがとう! – dferenc

関連する問題