하늘뚱

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

· 2023-03-14 (화) 13:34:52 · 2327 · 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,570
MySQL 23-07-02 조회 2,238
JavaScript 23-07-01 조회 1,872
PHP 23-06-30 조회 2,871
MySQL 23-06-29 조회 7,639
node.js 23-06-28 조회 1,906
node.js 23-06-27 조회 1,475
PHP 23-06-26 조회 1,705
PHP 23-06-25 조회 1,732
PHP 23-06-24 조회 1,210
PHP 23-06-22 조회 1,502
PHP 23-06-20 조회 1,575
기타 23-06-19 조회 1,842
23-06-17 조회 1,246
기타 23-06-18 조회 2,304
PHP 23-06-17 조회 1,601
JavaScript 23-06-16 조회 2,296
PHP 23-06-15 조회 1,969
23-05-16 조회 1,179
기타 23-06-13 조회 1,611
PHP 23-06-05 조회 1,699
PHP 23-05-28 조회 2,014
기타 23-05-23 조회 2,353
PHP 23-05-19 조회 1,824
PHP 23-05-18 조회 1,965
PHP 23-05-08 조회 1,926
JavaScript 23-04-10 조회 1,707
MySQL 23-04-08 조회 1,929
MySQL 23-03-15 조회 1,944
PHP 23-03-14 조회 2,328