View Full Version : number generator
james438
04-22-2012, 06:58 AM
I couldn't think of a good title, but I was hoping to get some help with a script that will calculate and display all possible number combinations of a 3 (or 4) digit number where the number of digits is 3, they are non repeating, and is limited to the numbers 1 - 5.
I imagine this would not be too big of a script, but how to do this is not coming to mind.
djr33
04-22-2012, 07:24 AM
Do you just want one at random? This will work for that:
function randnumstr() {
for($i=1;$i<5;$i++) {
$a[] = $i;
}
shuffle($a);
return implode('',$a);
}Of course you can chop that at 3 characters long if you want.
But if you want to actually systematically display all of them, then you should go through a loop.
There are two ways to do this: generate all possible numbers and eliminate those that have repeated digits; or generate only the valid ones. The first is easier to program, the second might be a little more efficient.
See if that gets you started. If not, I could probably write it for you.
james438
04-22-2012, 06:12 PM
Thank you for that. How would it look if you wanted to gather all of the combinations and listed them sequentially. A 3 digit combination is one idea, but what about 4 or 5? Most importantly, how would it look if it were not in a function? I mention that because I am terrible with functions in php. In javascript they are easier for me to understand for some reason.
Your code is giving me a good starting point and it is helping me to see how it would look.
There is no real application for this script. It's just a fun exercise.
djr33
04-22-2012, 08:21 PM
The function above is irrelevant. You could just take that wrapper off and use echo instead of return. Functions are just convenient.
For the other, you'd need to do a loop. It would get even more complicated if you wanted everything from 1-5 digits (rather than only 5 digits). You're going to end up with something like 4 embedded loops.
Alternatively you could use a recursive function. That'll be faster.
Here's a probably working example (untested):
$numbers = array();
function numberstuff($length,$prefix) { //this is ALL you need if you want every possible combination
if (!is_numeric($length)||$length<1||round($length)!=$length) { return false; }
for($i=0;$i<10;$i++) {
if ($length==1) {
numberstuff($length-1,$prefix.$i);
}
else {
$GLOBALS['numbers'][] = $prefix.$i . "\n";
}
}
}
function numberstuff2($length) { //this function strips out any numbers with duplicated numbers in them-- note that the max length now will be 9, because 10+ will contain duplicates and be removed
numberstuff($length);
$numbers = $GLOBALS['numbers'];
$GLOBALS['numbers'] = array(); //reset external storage array for later if needed
foreach($numbers as $nkey=>$n) {
for($i=0;$i<10;$i++) {
if (substr_count($n,$i)>1) {
unset($numbers[$nkey]);
}
}
}
sort($numbers); //clean up indices, etc.
return $numbers;
}
$mynumbers = numberstuff2(5);
print_r($mynumbers);
james438
04-23-2012, 01:31 AM
cool, thanks :)
I'll give it a whirl.
james438
04-25-2012, 06:31 AM
After looking at your script some more I wrote this:
<?php
$a=122;
$b=array();
while ($a<543){$a++;
$a1=substr($a,0,1);
$a2=substr($a,1,1);
$a3=substr($a,2,1);
if ($a3==6){$a2++;$a3=1;}
if ($a2==6){$a1++;$a2=1;}
$a="$a1"."$a2"."$a3";
if ($a>543)break;
if ($a1!=$a2 and $a1!=$a3 and $a2!=$a3)$b[]=$a;
}
echo"<pre>";
print_r($b);
echo"</pre>";
?>
I know it doesn't lend itself too well to versatility though.
djr33
04-26-2012, 04:30 AM
Haha. That's easier to deal with.
By chance, I'm working on a project tonight that needs a code like mine above. I need to find all possible combinatorial possibilities. It's very confusing. I'll try to adapt what I wrote above to make it work (and as I do so, I'll debug what I wrote, if there are problems).
djr33
04-26-2012, 04:37 AM
Well, that's hilarious. I used == instead of >. One typo. Now it works perfectly. Confusing to debug though... it was just giving 0-9 :p
(I updated a bit more too, mostly the output format. Now it returns a plain array of all the numbers.)
<?php
$numbers = array();
function numberstuff($length,$prefix='') { //this is ALL you need if you want every possible combination
if (!is_numeric($length)||$length<1||round($length)!=$length) { return false; }
for($i=0;$i<10;$i++) {
if ($length>1) {
numberstuff($length-1,($prefix.$i));
}
else {
$GLOBALS['numbers'][] = ($prefix.$i);
}
}
}
function numberstuff2($length) { //this function strips out any numbers with duplicated numbers in them-- note that the max length now will be 9, because 10+ will contain duplicates and be removed
numberstuff($length);
$numbers = $GLOBALS['numbers'];
$GLOBALS['numbers'] = array(); //reset external storage array for later if needed
foreach($numbers as $nkey=>$n) {
for($i=0;$i<10;$i++) {
if (substr_count($n,$i)>1) {
echo '1';
unset($numbers[$nkey]);
}
}
}
sort($numbers); //clean up indices, etc.
return $numbers;
}
$mynumbers = numberstuff2(4);
print_r($mynumbers);
?>
That works.
james438
04-26-2012, 05:18 AM
Yep, it is working for me now too. It was not working for me earlier, but I figured that was due to my lack of understanding of how to use php functions ;).
I noticed you left in a placeholder output in the first line of your output with "Array" listed at the end. Fun to try out by the way.
djr33
04-26-2012, 06:51 AM
Oops. Debug info now edited out.
james438
04-26-2012, 07:04 AM
You still have a little bit left :p
EDIT: Number of combinations = multiplying the highest numbers together. The number of numbers multiplied together is the same as the number of digits in the result. 4 digit result using 10 digits = 10*9*8*7=5040.
This reminds me of some algebra equation from my distant past. I am sure the equation I posted is flawed in some way, but it is interesting.
EDIT: Found it ref (http://www.mathwords.com/p/permutation_formula.htm).
djr33
04-27-2012, 06:53 AM
The number is easy enough to calculate mathematically, but actually finding all of the permutations directly without overgenerating* involves a ridiculous recursive function. It's a very hard task, although many other things aren't so hard.
(*Of course you can overgenerate and remove the extras as needed.)
It essentially needs to program itself. It's easy enough to write a set of embedded loops that will do it for a specific length, but allowing that to be any arbitrary length (as a parameter) is where it gets tricky.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.