Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Randomly select values from array
by BrowserUk (Patriarch) on May 16, 2010 at 09:34 UTC | |
selecting for of them ... no numbers left in the array Assuming that "for of them" means "four of them", then you have an immediate problem in that if your array contains 50 (or 51, or 53, or 54 or 55 etc. ) values, then you will eventually reach a point where there are less than four left to pick. What then? The ultimate purpose is to find all sort of combinations, Do you mean "all possible combinations"? If so, Do you mean "all possible sets of 4 that sum to 15" or "all possible sets of sets of 4 that sum to 15"? What if the array of numbers doesn't contain any set of four that sums to 15? Some code for you to play with. It generates an array of 50 small numbers (0..9) and then attempts to empty the array by picking and removing sets of 4. It will never terminate (use ^C), but might illustrate some ways of approaching whichever clarification of your problem is the correct one:)
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
Re: Randomly select values from array
by JavaFan (Canon) on May 16, 2010 at 10:41 UTC | |
Now what? | [reply] [d/l] [select] |
|
Re: Randomly select values from array
by Xilman (Hermit) on May 16, 2010 at 09:26 UTC | |
I'm guessing that "selecting for of them" is a typo for "selecting four of them". If not, please post a correction. Note that there may not be a way to remove all the numbers from the array. What if all the elements are negative, or greater than 15 say? Here is a sketch of just one way; there are many others, some of them more efficient by various metrics. First, sort the array randomly. Then see whether the first four elements sum to 15. If so, remove them. Keep going until you have an array with four or fewer elements or until you get bored. This code fragment performs the selection, testing and removal. Wrapping it in code to loop and, optionally, terminating the procedure is left as an exercise because I don't want to do all your work for you.
Paul | [reply] [d/l] |
by BrowserUk (Patriarch) on May 16, 2010 at 17:36 UTC | |
To emphasis how bad that sort-based "shuffle" is, consider that over a million shuffles of four values, 1/3rd of the possibilities are never chosen. And the rest are chosen so disproportionately that 'fairness' doesn't enter the equation:
Contrast that with
And the latter is more efficient to boot. Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
by ikegami (Patriarch) on May 16, 2010 at 17:51 UTC | |
| [reply] [d/l] [select] |
by blakew (Monk) on May 16, 2010 at 18:51 UTC | |
| [reply] [d/l] |
by BrowserUk (Patriarch) on May 16, 2010 at 19:11 UTC | |
by blakew (Monk) on May 16, 2010 at 19:37 UTC | |
by Xilman (Hermit) on May 17, 2010 at 08:04 UTC | |
"To emphasis how bad that sort-based "shuffle" is, consider that over a million shuffles of four values, 1/3rd of the possibilities are never chosen. And the rest are chosen so disproportionately that 'fairness' doesn't enter the equation:" I must try to remember that some people have difficulty recognizing meiosis (not that I'm including BrowserUk in that list). My original post included the words: "some of them more efficient by various metrics". Oh well. Paul | [reply] |
by BrowserUk (Patriarch) on May 17, 2010 at 08:52 UTC | |
by ikegami (Patriarch) on May 16, 2010 at 17:06 UTC | |
is not guaranteed to work, much less return something random. Use List::Util's shuffle instead. | [reply] [d/l] [select] |
|
Re: Randomly select values from array
by ikegami (Patriarch) on May 16, 2010 at 19:14 UTC | |
I'm not completely happy with it because it can find solutions that are subsets of previous solutions (although the $leaf code removes a lot of those).
| [reply] [d/l] [select] |