Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

8 Unique Numbers

Posted on 2008-06-23
7
Medium Priority
?
220 Views
Last Modified: 2008-07-01
Hello,

I need to randomly generate 8 UNIQUE random numbers.

No number may be used more than once.

So this number is good:
    53962471

This number is NOT because some numbers are duplicated:
    11223344


What is the shortest/simplest way to do this?
0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 24

Expert Comment

by:glcummins
ID: 21846706
Not sure that this method is the simplest, but it will definitely work and is extensible in case your requirements change:


<?php
 
$uniqueNumbers = 8;
$highestValue = 9;
$lowestValue = 1;
 
$arrGeneratedNumbers = array();
 
for ($i = 0; $i<$uniqueNumbers; $i++)
{
	$foundUnique = false;
	
	while (!$foundUnique)
	{
		$newNumber = rand($lowestValue, $highestValue);
		if (!in_array($newNumber, $arrGeneratedNumbers))
		{
			$arrGeneratedNumbers[] = $newNumber;
			$foundUnique = true;
		}
	}
}
 
foreach ($arrGeneratedNumbers as $number)
{
	echo $number;
}
 
?>

Open in new window

0
 
LVL 24

Assisted Solution

by:glcummins
glcummins earned 1200 total points
ID: 21846735
Here is a simpler method, but it limits you to the eight numbers that you enter into the array:
<?php
 
$arrNumbers = array(1,2,3,4,5,6,7,8);
 
shuffle($arrNumbers);
 
foreach ($arrNumbers as $number)
{
	echo $number;
}
 
?>

Open in new window

0
 
LVL 14

Assisted Solution

by:agriesser
agriesser earned 400 total points
ID: 21846818
Try this.
<?php
 
$magicnumbers = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$randomnumber = '';
 
for($i = 0; $i < 8; $i++)
{
  $r = rand(0, 9 - $i);
  $randomnumber .= $magicnumbers[$r];
  unset($magicnumbers[$r]);
  $magicnumbers = array_values($magicnumbers);
}
 
echo $randomnumber;
 
?>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Assisted Solution

by:spoxox
spoxox earned 400 total points
ID: 21846981
Is there a requirement for the length of the random numbers? Assuming NO
Are all digits 0-9 eligible? Assuming YES
Is zero acceptable? Assuming YES
<?php
$uNums = array();
$digits = array(1,2,3,4,5,6,7,8,9,0);
$req = 8;
$nCount = 0; 
while ($nCount < $req) {
  shuffle($digits); // randomly shuffle array of digits
  $nLen = rand(1,10);  // determine length of next random digit string
  $num = ""; // initialize digit string
  for ($i = 0; $i < $nLen; $i++) { // collect $nLen unique digits
    $num = $num.$digits[$i];
  }
  if (!in_array($num, $uNums)) { // verify digit string/number is not in array
    $uNums[] = $num; // add digit string/number to array
    $nCount++;
  }
}
?>

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 21847104
I feel silly answering my own question but the ideas keep getting more complex.

One of your ideas triggered a lightening bolt in my head and I came up with this.

What do you think?
<?php
 $str = '0123456789';
 $shuffled = str_shuffle($str);
 echo substr($shuffled,-8);
?>

Open in new window

0
 
LVL 24

Expert Comment

by:glcummins
ID: 21847129
Yes, that will work, and will allow you to start with as large a data set as desired.
0
 
LVL 16

Accepted Solution

by:
hankknight earned 0 total points
ID: 21847132
Or even better:
<?php
echo substr(str_shuffle('0123456789'),-8);
?>

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question