Random numbers and combinations

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ccjob2
Forum Newbie
Posts: 16
Joined: Tue Jun 04, 2002 9:37 am

Random numbers and combinations

Post by ccjob2 »

Hi,
I have to define a routine that extract some number (i.e. 9 numbers), into a range (i.e. from 2 to 65) and makes a combination of those numbers each (i.e.) 5 of all extract.
Is there some PHP functions that build that routine ? Or some scripts that you can indicate ?
If there is anyone that help me on that routine I thank very very much !!!

Thanks
Carlo 8O
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

The function rand() will generate a random number (or at least one taken for computer clock ticks or w/e) between your parameter...such as:

Code: Select all

rand(1, 20)
That will get a random number between 1 and 20...very simple.....i suppose you could use that in like a loop, of do it 9 times :P
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

srand((double) microtime() * 1000000); 
$num = 0;
for($i = 0; $i < 5; $i++)&#123;
$num = $num + rand(2, 65);
&#125;
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

That works much better than doing it 9 times...lol......im just HORRIBLE with loops.....
ccjob2
Forum Newbie
Posts: 16
Joined: Tue Jun 04, 2002 9:37 am

The combination is the problem

Post by ccjob2 »

Thanks for yours suggestions, but my problem is the combination of number series.
If I extract (i.e.) 30 numbers (random), I should combine all numbers and produce all available combinations of those numbers in groups of 5 numbers (i.e.) each.
For example:
Extraction: 3 56 4 55 22 33 44 78 90 43 2 .....
If I need to create groups of 5 numbers each, how many group may I create ? And which combinations ?
Example:
1th combination: 3 56 4 55 22
2nd combination: 3 56 4 55 33
3rd combination: 3 56 4 55 44
etc. etc.
To make that is there some routine already made on the net ?
Thanks for all !!!

Carlo
Post Reply