Jump to content

random unique numbers


saltedm8
 Share

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)

 

I have made an attempt,

<?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

Link to comment
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

Link to comment
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 />';
}

Link to comment
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

Link to comment
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

Link to comment
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

Link to comment
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.

Link to comment
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

Link to comment
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>
<head>
<title>Check my Numbers</title>
</head>
<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>

Link to comment
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.