하늘뚱

1에서 45까지 랜덤이면서 연속 숫자도 가능한 소스

· 2023-03-14 (화) 13:34:52 · 2357 · 3

만약에 연속 숫자도 가능하면서 1 ~ 45 까지 랜덤한 6자리를 10번씩 뽑고자 한다면

[code]

<?php
for ($i = 0; $i < 10; $i++) {
  $selected_numbers = array();
  while (count($selected_numbers) < 6) {
    $number = rand(1, 45); // generate a random number between 1 and 45
    if (!in_array($number, $selected_numbers)) { // check if number is already selected
      if (count($selected_numbers) > 0 && $number == $selected_numbers[count($selected_numbers) - 1] + 1) {
        $selected_numbers[] = $number; // add number to selected numbers if consecutive
      } else {
        $selected_numbers = array(); // reset the array if non-consecutive number is found
      }
    }
  }
  // Print the selected numbers for this set
  echo "Set " . ($i + 1) . ": " . implode(" ", $selected_numbers) . "<br>";
}
?>
[/code]

|

댓글 3개

2023-03-30 (목) 05:50:34
처음 숫자가 44쯤 되면 무한 루프가 되지 않을까요
@엑스엠엘 이게 제가 곰곰하게 생각한 것이 아니라 인공지능을 가미해서 만들려다 랜덤으로 했습니다.10번찍고 빠져나오죠.
2023-05-04 (목) 05:01:34
$number = rand(1, 45);
for( $i=0; $i<6; $i)
$selected_numbers[]=$number+$i;
이런 결과를 말씀하시는 건가요?
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,403건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
기타 23-07-03 조회 1,811
MySQL 23-07-02 조회 2,273
JavaScript 23-07-01 조회 1,917
PHP 23-06-30 조회 2,911
MySQL 23-06-29 조회 7,664
node.js 23-06-28 조회 1,935
node.js 23-06-27 조회 1,495
PHP 23-06-26 조회 1,719
PHP 23-06-25 조회 1,754
PHP 23-06-24 조회 1,227
PHP 23-06-22 조회 1,518
PHP 23-06-20 조회 1,614
기타 23-06-19 조회 1,879
23-06-17 조회 1,260
기타 23-06-18 조회 2,321
PHP 23-06-17 조회 1,615
JavaScript 23-06-16 조회 2,315
PHP 23-06-15 조회 1,988
23-05-16 조회 1,194
기타 23-06-13 조회 1,623
PHP 23-06-05 조회 1,715
PHP 23-05-28 조회 2,034
기타 23-05-23 조회 2,368
PHP 23-05-19 조회 1,837
PHP 23-05-18 조회 1,980
PHP 23-05-08 조회 2,025
JavaScript 23-04-10 조회 1,720
MySQL 23-04-08 조회 1,945
MySQL 23-03-15 조회 1,963
PHP 23-03-14 조회 2,358