100% 꽝을 보장하는 간단한 로또번호 생성 함수
[code]
function select_number($number=array()) {
if(!empty($number) && count($number) === 6) {
return $number;
}
$rand = rand(1,45);
if(empty($number)) {
array_push($number,$rand);
return select_number($number);
} else if( (!in_array($rand,$number) && count($number) < 6)) {
array_push($number,$rand);
return select_number($number);
} else {
return select_number($number);
}
}
$number = ['3','6'];
$select_number = select_number($number);
[/code]
$number 배열에 미리 지정하고 싶은 숫자를 넣어놓으면, 해당 숫자를 포함해서 생성합니다.
지난주 심심해서 사봤는데 꽝이더라구요
댓글 4개
WEBOLUTION
2년 전
[code]function select_numbers($selected_numbers = []) {
$num_numbers_to_select = 6 - count($selected_numbers);
while ($num_numbers_to_select > 0) {
$rand = rand(1, 45);
if (!in_array($rand, $selected_numbers)) {
$selected_numbers[] = $rand;
$num_numbers_to_select--;
}
}
sort($selected_numbers);
return $selected_numbers;
}
$selected_numbers = select_numbers([3, 6]);[/code]
이렇게도 될듯합니다 ^^;
$num_numbers_to_select = 6 - count($selected_numbers);
while ($num_numbers_to_select > 0) {
$rand = rand(1, 45);
if (!in_array($rand, $selected_numbers)) {
$selected_numbers[] = $rand;
$num_numbers_to_select--;
}
}
sort($selected_numbers);
return $selected_numbers;
}
$selected_numbers = select_numbers([3, 6]);[/code]
이렇게도 될듯합니다 ^^;
WEBOLUTION
2년 전
[code]function select_numbers($numbers = []) {
while (count($numbers) < 6) {
$rand = rand(1, 45);
if (!in_array($rand, $numbers)) $numbers[] = $rand;
}
sort($numbers);
return $numbers;
}
$lotto_numbers = select_numbers([1, 3]);[/code]
간단 버전~
while (count($numbers) < 6) {
$rand = rand(1, 45);
if (!in_array($rand, $numbers)) $numbers[] = $rand;
}
sort($numbers);
return $numbers;
}
$lotto_numbers = select_numbers([1, 3]);[/code]
간단 버전~
2년 전
메이드니 간만에 팁 올리셨는데 추천 드립니다.
2년 전
@비타주리 항상 비타님 js 보면서 활력을 얻습니다~
게시판 목록
개발자팁
개발과 관련된 유용한 정보를 공유하세요.
질문은 QA에서 해주시기 바랍니다.
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 5306 | JavaScript |
niceguys
|
2년 전 | 1225 | |
| 5305 | JavaScript |
niceguys
|
2년 전 | 1120 | |
| 5304 | PHP |
niceguys
|
2년 전 | 748 | |
| 5303 | JavaScript |
|
2년 전 | 1314 | |
| 5302 | PHP |
|
2년 전 | 809 | |
| 5301 | JavaScript |
techstar
|
2년 전 | 620 | |
| 5300 | PHP |
techstar
|
2년 전 | 675 | |
| 5299 | jQuery | 2년 전 | 702 | ||
| 5298 | JavaScript | 2년 전 | 637 | ||
| 5297 | 기타 |
시우시우9082
|
2년 전 | 1114 | |
| 5296 | 기타 |
|
2년 전 | 802 | |
| 5295 | PHP | 2년 전 | 1139 | ||
| 5294 | JavaScript |
swallow
|
2년 전 | 1905 | |
| 5293 | JavaScript |
swallow
|
2년 전 | 820 | |
| 5292 | JavaScript |
swallow
|
2년 전 | 1009 | |
| 5291 | JavaScript |
swallow
|
2년 전 | 698 | |
| 5290 | PHP |
swallow
|
2년 전 | 800 | |
| 5289 | PHP |
swallow
|
2년 전 | 820 | |
| 5288 | JavaScript |
swallow
|
2년 전 | 834 | |
| 5287 |
swallow
|
2년 전 | 1375 | ||
| 5286 | 2년 전 | 1583 | |||
| 5285 | 2년 전 | 1309 | |||
| 5284 | 2년 전 | 1725 | |||
| 5283 | 2년 전 | 1750 | |||
| 5282 | 2년 전 | 1441 | |||
| 5281 |
|
2년 전 | 1741 | ||
| 5280 | 2년 전 | 1174 | |||
| 5279 | 2년 전 | 1813 | |||
| 5278 | JavaScript | 2년 전 | 1614 | ||
| 5277 | 2년 전 | 1409 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기