# random unique numbers

## Recommended Posts

hi, I am trying to create 7 lines, out of 49 numbers, each line must have unique numbers not seen in any other line so that every number out of 49 has been used in one of those lines, I also need them to be in order, lowest to highest except for the last number which needs to be completely any random ( as long as its not elsewhere in the 7 lines and its one of 49)

<?php

function code(){
for (\$i=1; \$i<=7; \$i++){
\$one = mt_rand(1, 49);
echo \$one;
echo ' ';
}}

function lines(){
for (\$i=1; \$i<=7; \$i++){
code();
echo '<br/>';
}}

lines();

?>

but I am seriously failing at producing every number from 1 to 49 throughout those 7 lines and of course failing at ordering them with the random at the end that is not in order..

sorry if this sounds confusing but I would appreciate any help, thanks

##### Share on other sites

<?php
\$rows = 7;
\$columns = 7;
\$numbers = range(1,(\$rows*\$columns));
shuffle(\$numbers);

for(\$row = 0; \$row<\$rows; \$row++){
\$this_row = array();
for(\$column =0; \$column<(\$columns-1); \$column++){
\$this_row[] = array_shift(\$numbers);
}
sort(\$this_row);
\$this_row[] = array_shift(\$numbers);
echo implode(\$this_row, ' ');
echo '<br />';
}
?>

Not tested

Edit: Saw the other requirement and made changes

##### Share on other sites

wow, that is so wicked, thank you very much, I still have a lot to learn when it comes to php

##### Share on other sites

I guess I'm late to the party, but it was kind of fun so I'll post mine anyway.

<?php

function rand_num()
{
// create array of numbers
\$pool = range(1,49);

// initialize the array we will be outputting
\$rand_nums = array();

// we need to create 7 lines, so lets loop 7 times
for(\$i = 0; \$i < 7; \$i++)
{
// now we need to add 7 items to each line
// so lets loop another 7 times
for(\$x = 1; \$x <= 7; \$x++)
{
// get a random index from the array of numbers
\$rand = array_rand(\$pool);

// add it to the line
\$rand_nums[\$i][] = \$pool[\$rand];

// delete the index from the array
// so it cannot be used again
unset(\$pool[\$rand]);
}
}

// return the array of unique random numbers
return \$rand_nums;
}

\$rand = rand_num();

// loop through the random numbers to print 7 per line
foreach(\$rand as \$nums)
{
sort(\$nums);

echo implode(', ', \$nums) . '<br />';
}

##### Share on other sites

lol, thank you too, they both work a treat.. maybe I will use both, thanks again

##### Share on other sites

I guess I'm late to the party, but it was kind of fun so I'll post mine anyway.

That's exactly why I did it, it was a fun little challenge. I think I edited mine at least a dozen times to make it "better". I was trying to get down to as few lines as possible lol

##### Share on other sites

If you both enjoyed that I could let you know my next step

I need to take those numbers and match them against manually entered numbers in another file, if any of those lines ( the first 6 numbers ) sequence matches a sequence in that file I need it to randomly change again.

it would be ok to have a warning to refresh the page if you wanted to do it like that if it found a match

##### Share on other sites

Are you generating lotto tickets?

##### Share on other sites

yes, but its for a client who wants it as a feature on their site.. I personally don't even play it, waste of money

he wants to base it off of mathematical theory that if you use every number every week eventually you have to win, or something like that.. sounds silly but as he is the paying client, I am not going to complain

##### Share on other sites

What's the format of the file?

##### Share on other sites

it does not matter, he wants to only have to open it and paste it in, I was thinking about an array

##### Share on other sites

With all due respect this seems like a perfect scenario for array_chunk()

\$numbers = range(1, 49);
shuffle(\$numbers);
\$picks = array_chunk(\$numbers, 7);
foreach(\$picks as \$pick)
{
sort(\$pick);
echo implode(', ', \$pick) . "<br>\n";
}

As for this:

I need to take those numbers and match them against manually entered numbers in another file, if any of those lines ( the first 6 numbers ) sequence matches a sequence in that file I need it to randomly change again.

it would be ok to have a warning to refresh the page if you wanted to do it like that if it found a match

How many picks will exist in the list? As the list grows the chances of matches will increase. Over time the number of iterations needed to find a result with no matches will grow.

##### Share on other sites

6 months, maybe a year,

2 lottery draws a week 52 weeks in a year = 104 combinations + combinations already used max 104 = 208 combinations at the end of the year within that file, after that as one is added an older one is removed

that's how I understood it anyway

The way it was explained to me was that the chances of the same combination popping its head again are something like 45 million to one

##### Share on other sites

for the array_chunk that does work better, but you'd want to do 6 and then add one random one on the end, according to the OP.

##### Share on other sites

and order them lol

##### Share on other sites

Ninja'd on the array_chunk!

His theory is bonkers, but that's none of our concern.

<?php

\$results = FALSE;

# Check if the page was requested via POST, and if our textarea is set and not empty
if( \$_SERVER['REQUEST_METHOD'] == 'POST' && isset(\$_POST['numbers']) && !empty(\$_POST['numbers']) ) {

\$rows = 7;
\$columns = 7;
\$numbers = range(1,(\$rows*\$columns));

do { # Begin loop.
shuffle(\$numbers); # Mix up numbers
\$lines = array_chunk(\$numbers, \$rows); # Split them into '7' parts
foreach( \$lines as &\$line ) { # Loop through the lines, keeping any changes made to the array
\$end = array_shift(\$line); # Grab the first value, and remove it from the array
sort(\$line); # Sort the values numerically
\$line[] = \$end; # Add the extracted value to the end
}
unset(\$line); # Destroy potentially annoying reference

\$posted_lines = explode("\n",\$_POST['numbers']); # Explode the posted data on new lines
foreach( \$posted_lines as &\$posted_line ) { # Loop through the lines, keeping any changes made to the array
\$posted_line = trim(\$posted_line); # Remove any whitespace from the start and end of the line
\$posted_line = preg_replace('#\s+#', ' ', \$posted_line); # Get rid of multiple spaces between numbers
# Not the best way to do this, but I want to avoid using looped string functions for presentation's sake
\$posted_numbers = explode(' ', \$posted_line); # Explode on spaces
\$posted_numbers = implode(' ', array_slice(\$posted_numbers,0,6)); # Implode only first 6 results
foreach( \$lines as \$line ) { # Loop through generated lines
if( implode(' ',array_slice(\$line,0,6)) == \$posted_numbers ) # Check if any first 6 match
continue 3; # If so, break out of both foreach loops, and continue at the start of the do {
}
}
unset(\$posted_line); # Destroy potentially annoying reference
\$results = TRUE; # Let our script know we've found results
} while( !\$results );
}

?>
<!DOCTYPE html>
<html>
<title>Check my Numbers</title>
<body>
<form action="" method="post">
<div>
<h3>Format for posting</h3>
At least 6 numbers per row, from 1 to 49, separated by a space.<br>
Each row separated by a line break.<br>
<span style="font-family: monospace; background-color: lightgrey;">
8 22 28 32 38 40 47<br>
10 13 17 23 26 31 42
</span><br>
<textarea style="width: 200px; height: 200px; font-family: monospace;" name="numbers"></textarea><br>
<input type="submit">
</div>
</form>
<?php
if( \$results != FALSE ) {
echo '<h3>Unique list of '.\$rows.' combination(s)</h3><pre>Generated Results:'."\n";
foreach( \$lines as \$line ) {
echo implode(' ',\$line)."\n";
}
echo '</pre><pre>Posted data:'."\n".implode("\n",\$posted_lines);
}
?>
</body>
</html>

##### Share on other sites

lol, I completely agree lol

thanks for that, I will dress it up a little and shove it in his design

##### Share on other sites

and order them lol

that would be what the sort() was for.

##### Share on other sites

This thread is more than a year old.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.