/    Sign up×
Community /Pin to ProfileBookmark

Lotto number generator help

I’ve been trying to make a script that generates random lotto numbers. The problem is, the numbers generated, aren’t always unique. Sometimes the same number is rolled twice. Any Ideas?

[code=php]
<?php
$n1 = mt_rand(1,47);
$n2 = mt_rand(1,47);
$n3 = mt_rand(1,47);
$n4 = mt_rand(1,47);
$n5 = mt_rand(1,47);

$bonus = mt_rand(1,27);

print “<p>$n1, $n2, $n3, $n4, $n5, <span style=”color:red”>$bonus</span></p>n”;
?>
[/code]

Example: [url]http://vintagecloning.host.sk/lotto.php[/url]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceMar 19.2005 — You should, one at a time, make temporary picks into an array. As the temporary pick is made, it must be checked against all permanent picks already in the array. If not found, that temporary pick becomes a permanent pick and selection moves on to the next temporary pick. If a temporary pick is found to have already been picked previously, then that temporary pick is discarded and a new temporary pick made in its place. The process continues until all picks have been made.
Copy linkTweet thisAlerts:
@JoshauthorMar 22.2005 — [i]Originally posted by phpnovice [/i]

[B]You should, one at a time, make temporary picks into an array.[/B][/QUOTE]

So how do I insert rand() numbers into an array()? This doesn't seem to be working...
[code=php]
$r1 = mt_rand(1,47);
$r2 = mt_rand(1,47);
$r3 = mt_rand(1,47);
$r4 = mt_rand(1,47);
$r5 = mt_rand(1,47);

$numbers = "$r1 $r2 $r3 $r4 $r5";
$array = explode(" ", $numbers);

printf($array);
[/code]

[b]EDIT:[/b] I solved this problem.
[code=php]
print_r($array);
[/code]
Copy linkTweet thisAlerts:
@DARTHTAMPONMar 22.2005 — [code=php]
#requires: nothing
#modifies: $r
#effects : inserts 5 unique numbers between 1 and 47 into $r array

for ($x = 0; $x < 5; $x++)
{
$number = mt_rand(1,47);
for ($y = 0; $y < $x; $y++) #run through existing numbers
{
if ($number == $r[$y]) #check to see weather they exist yet
{
$x = $x - 1; #subtract 1 from $x to rerun loop
break;
}
else
{
$r[$x] = $number; #if number is not the same insert into $r
}
}
}

[/code]


this should work
Copy linkTweet thisAlerts:
@NogDogMar 22.2005 — Example slightly modified from http://us4.php.net/manual/en/function.mt-rand.php :
[code=php]
$randomNumbers = array(); // storage array
for ($i=0;$i<5;$i++) {
$random = mt_rand(1,47);
while(in_array($random,$randomNumbers)) {
$random = mt_rand(1,47); }
array_push($randomNumbers,$random);
}
print_r($randomNumbers);
[/code]
Copy linkTweet thisAlerts:
@JoshauthorMar 23.2005 — [i]Originally posted by NogDog [/i]

[B]Example slightly modified from http://us4.php.net/manual/en/function.mt-rand.php :

[code=php]
$randomNumbers = array(); // storage array
for ($i=0;$i<5;$i++) {
$random = mt_rand(1,47);
while(in_array($random,$randomNumbers)) {
$random = mt_rand(1,47); }
array_push($randomNumbers,$random);
}
print_r($randomNumbers);
[/code]
[/B][/QUOTE]

Your code worked fine without a hitch by adding...
[code=php]
print $randomNumbers[0] . "n";
print $randomNumbers[1] . "n";
print $randomNumbers[2] . "n";
print $randomNumbers[3] . "n";
print $randomNumbers[4] . "n";
[/code]

Although I don't quite understand...
[code=php]
$i=0;$i<5;$i++
[/code]

Test: http://usuarios.lycos.es/vintagecloning/lotto-beta3.php
Copy linkTweet thisAlerts:
@JoshauthorMar 23.2005 — [i]Originally posted by DARTHTAMPON [/i]

[B][code=php]
#requires: nothing
#modifies: $r
#effects : inserts 5 unique numbers between 1 and 47 into $r array

for ($x = 0; $x < 5; $x++)
{
$number = mt_rand(1,47);
for ($y = 0; $y < $x; $y++) #run through existing numbers
{
if ($number == $r[$y]) #check to see weather they exist yet
{
$x = $x - 1; #subtract 1 from $x to rerun loop
break;
}
else
{
$r[$x] = $number; #if number is not the same insert into $r
}
}
}

[/code]


this should work [/B][/QUOTE]

After adding this...
[code=php]
print $r[1] . "n";
print $r[2] . "n";
print $r[3] . "n";
print $r[4] . "n";
print $r[5] . "n";
[/code]

I found that only four random numbers are displayed. Although if I change this...
[code=php]
$x = 0; $x < 5; $x++
[/code]

to this...
[code=php]
$x = 0; $x < 6; $x++
[/code]

I get the desired result.

Test: http://usuarios.lycos.es/vintagecloning/lotto-beta4.php
Copy linkTweet thisAlerts:
@JoshauthorMar 23.2005 — [i]Originally posted by Josh [/i]

[B]I found that only four random numbers are displayed.[/B][/QUOTE]

I had the same problem here using array_rand().
[code=php]
$array = array (1 =>
'1','2','3','4','5','6','7','8','9','10',
'11','12','13','14','15','16','17','18','19','20',
'21','22','23','24','25','26','27','28','29','30',
'31','32','33','34','35','36','37','38','39','40',
'41','42','43','44','45','46','47'
);

$rand_keys = array_rand($array, 5);

$n1 = $array[$rand_keys[1]];
$n2 = $array[$rand_keys[2]];
$n3 = $array[$rand_keys[3]];
$n4 = $array[$rand_keys[4]];
$n5 = $array[$rand_keys[5]];

print "$n1, $n2, $n3, $n4, $n5,";
[/code]

But if I change..
[code=php]
$rand_keys = array_rand($array, 5);
[/code]

to...
[code=php]
$rand_keys = array_rand($array, 6); // or more
[/code]

it works.

Test: http://usuarios.lycos.es/vintagecloning/lotto-beta2.php

What's going on here and how do I fix it?
Copy linkTweet thisAlerts:
@NogDogMar 23.2005 — [i]Originally posted by Josh [/i][b]

...

Although I don't quite understand...

[code=php]
$i=0;$i<5;$i++
[/code]

[/B][/QUOTE]

That's just a loop construct to make it iterate 5 times:

$i=0 : set counter $i to 0

$i<5 : if $i is less than 5 then execute the loop

$i++ : increment the value of $i by one upon completion of the loop
×

Success!

Help @Josh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...