Help me create a grouping algorithm

Onund

Senior member
Jul 19, 2007
287
0
0
I want to be able to take a list of random numbers and split them into a known number of groups. The trick is I don't always need each group to contain a number but sometimes require particular groups to contains numbers. Also, it would be best if the numbers were not distributed symmetrically among the available groups. Repeatability of the grouping is crucial. If given the same set of numbers, they must always fall in the same groups.

I have an algorithm for splitting my list of numbers when there is never a required bucket, called modulo ;), but get hung up when a bucket is required to have a number.

So, a typical example would be 10 random numbers and 4 group, 3 of which must contain at least 1 number, the last group may or may not get numbers.

I can't think of a good way of doing this. All the things I thought up either evenly divide among the groups, including the non-required one, or don't work, or kinda suck.

There has to be some math theory that lets you take a random assortment of numbers and map it to some ranges...

anyone?
 

QED

Diamond Member
Dec 16, 2005
3,428
3
0
Just make your "non-required" group accept only numbers that are primer, or are divisble by 7, or are perfect squares, or some other property which is only true for certain integers.

Now your problem is reduced to distributing the remaining numbers among your "required" groups-- which each group getting at lest one number. You could achieve this somewhat artificially by assigning, for example, the first 3 numbers into the 3 required groups, and then distribute the remaining numbers based on their value modulo 3.

 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
I think you'll have a hard time finding a function mapping of [number]->[bucket] that guarantees a particular set of buckets will be filled when [number] can be random. I think you should listen to QED's approach, and use the first few elements in the list to provide a single member to each 'required' bucket, then fall back on modulo N or some other hash for the rest of the elements.
 

presidentender

Golden Member
Jan 23, 2008
1,166
0
76
Originally posted by: degibson
I think you'll have a hard time finding a function mapping of [number]->[bucket] that guarantees a particular set of buckets will be filled when [number] can be random. I think you should listen to QED's approach, and use the first few elements in the list to provide a single member to each 'required' bucket, then fall back on modulo N or some other hash for the rest of the elements.

Assign the first 3 numbers to buckets, making sure there is no repetition. Then programmatically build a rule for each bucket based on its contents. That's hard, but I imagine it is doable.

edit: the point of doing it that way is to make sure that the all the numbers in each bucket follow the same rules. If that's not needed, just put the first few in the 'required' buckets and call it good, like everyone else said.
 

Onund

Senior member
Jul 19, 2007
287
0
0
thanks for the replies. I had already thought of doing the same thing, assigning the first n elements to the n required buckets then just modulo the rest into all the buckets. I was hoping there would be a better way to distribute the numbers around.

For my purposes it would be ok to have repeated numbers going to different buckets. I'll just go with that then.