Junilu Lacar wrote:2. The source list must have at least N elements.
Picking random items
Junilu Lacar wrote:3. The new list elements must not be in the same order as they are in the source list.
Note overflow error at 21. I should have written 1L rather than 1.My JShell wrote:jshell> long factorial(int i)
...> {
...> return i == 0 ? 1 : i * factorial(i - 1);
...> }
| created method factorial(int)
jshell> factorial(21)
$2 ==> -4249290049419214848
jshell> factorial(20)
$3 ==> 2432902008176640000
Campbell Ritchie wrote:only allow duplicates in the destination if the source contains duplicates, and no more of them than in the sourtce.
Liutauras Vilda wrote:
Picking random items
Hm.. This requirement also makes me stop for a while. Because now we kind of limiting randomization if I could say like that, sort of have to influence it in some sense.Junilu Lacar wrote:3. The new list elements must not be in the same order as they are in the source list.
Paul Clapham wrote:Requirement 3 is a bit sketchy given the possibility of duplicates. Or N=1. But how about this:
1. Copy the original list into a new list.
2. Shuffle that new list randomly.
3. Truncate it to the first N elements.
But I must be missing something because the SO posters were fighting with much more complicated solutions.
Paul Clapham wrote:Requirement 3 is a bit sketchy given the possibility of duplicates. Or N=1. But how about this:
1. Copy the original list into a new list.
2. Shuffle that new list randomly.
3. Truncate it to the first N elements.
But I must be missing something because the SO posters were fighting with much more complicated solutions.
BogoSort will produce a sorted list if you wait long enough.Junilu Lacar wrote:. . . the Bogo sort algorithm could produce a sorted list.
Campbell Ritchie wrote:BogoSort will produce a sorted list if you wait long enough.
Thank you; I was obviously mistaken, or confused. I hope I got the right number of 9s in my percentage, That shows the chances of it running in O(∞) time with a 46‑but randomness generator.Stephan van Hulst wrote:. . . it randomizes the list. If it doesn't, it's not Bogosort.
Campbell Ritchie wrote:I hope I got the right number of 9s in my percentage,
Campbell Ritchie wrote:That shows the chances of it running in O(∞) time with a 46‑but randomness generator.
Damn! I went from 6.4 to 4.6.Mike Simmons wrote:. . . I'm going to have to take off points on your landing, there. . . .
Campbell Ritchie wrote:You need a much longer seed to have any chance of keeping up with 52!. Or even 25!; bogosort for a half‑pack of cards would take many times longer than the known age of the Universe.
When I searched for bogosort earlier today, I think I did see mention of a quantum algorithm. It obviously changes the algorithm from an ordinary spaceship to the Heart of Gold.Mike Simmons wrote:. . . . Unless there's the possibility of a quantum computing bogosort . .
Campbell Ritchie wrote:When I searched for bogosort earlier today, I think I did see mention of a quantum algorithm. It obviously changes the algorithm from an ordinary spaceship to the Heart of Gold.