New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add 'F_ShuffleNumbers' as Global Function #872
Conversation
And are you dissatisfied with me, onee-sama? ;) This algorithm is very interesting, I had to read it a few times to figure out what it was doing. Let's take for example (I'll pick a simple one just to show this, but it also happens with other inputs) the call When the function is called with those arguments, both
Which will produce the following outputs: (I'll skip the steps and the intermediate values of the variables during the loop, I'm sure everyone would be bored and I'd get a cramp by typing them)
Do you have any more details about this algorithm? Where was it described? Did KeyWorld come up with it, or it's based on something known? Ideally, we'd want an algorithm without the bias this one has (and that can produce every result)... but I can't think of a way that's as efficient as this one (and it's bothering me to no end :D). If the aim is just to pick k elements from a list of N elements (with k much smaller than N), then the solution is simple and we could leverage the sparseness of the Hercules arrays with something similar to:
but that becomes severely inefficient as k gets closer to N (unbounded time). In any case, if we want to keep this algorithm despite the bias, it should be noted in a comment, so that people will know what they're using. By the way, on an unrelated note, you might want to replace |
OH MY ! Haru-sama, I'm sooo happy in fact
AHHHHH !!! now have to throw it away
the best shuffle algorithm to shuffle whole array is Fisher Yates shuffle
no, now I know that shuffle algorithm give bias result, I wont use it anymore
I got it from here keyworld always invent new stuffs and new technique, always amazed us http://upaste.me/59b622000817023d1 EDIT: I made a mistake on the last one =/ |
so the non-bias shuffle algorithm, that gives consistent result
yours and keyworld's one are quite near, so let me test it on the performance http://upaste.me/3e21220016ec96594 yours = 78 so I guess yours better EDIT: err ... need more performance test ... wait ... so I guess has to stick to yours =/ |
change to haru's snippet available
forgot the whitespace ...
tested with
http://adrianquark.blogspot.my/2008/09/how-to-shuffle-array-correctly.html
still can give every one of the 24 combinations correctly |
My algorithm and KeyWorld's are very similar, but mine uses arrays (which are very optimized in the Hercules scripting engine), while his uses string operations. Operations on strings are always slow, and The problem in both, is that they require an unbounded amount of draws, and it gets worse and worse when the array is very large and you want to shuffle it completely. It's not a problem if you only need to draw a relatively small amount of shuffled numbers, but as the size of the output and the size of the input get closer, the chance to generate a "new" number gets very small. In defense of Fisher-Yates, it should perform well when shuffling large sets completely. The only bottleneck I see is the partial
Instead of removing elements from the middle of the array, I'm replacing the used elements with the last one (then optionally removing the last element -- that's much faster than removing from the middle, but even so not necessary, so I commented it out). That should perform better. |
Here's the test on my laptop (took the average out of 100 iterations of each algorithm -- results seem more consistent when I repeat it): Each result set shows, in order, Haru_shuffle, keyworld_inf_shuffle, modern_fisher_yates, modern_fisher_yates_haru
Test environment was:
|
yeah I already tested it before this post came out erm .. now I re-read the wiki again. yours that slashed out the index is modern method EDIT: of course I tested with the script in post ... |
change again to Fisher Yate's shuffle modern method
- ... fix the example function name ...
the problem is ... now the script in |
eathena forum up |
Wow, truly amazing. That's surely an elegant and compact way to check a sudoku solution :o |
Merged as bb214d4, thank you! |
@MishimaHaruna
you are the only one left can play algorithm script with me =/
I kinda like to add this function into svn
have been play with
I prefer this function made by keyworld over this
http://www.fredosaurus.com/notes-cpp/misc/random-shuffle.html
because the above link will ask you to create a temporary array length equal to max-min
like the script I wrote, there is about 9000+ entry from itemDB
this one made by keyworld will only loop as few as how many index the user need
and BEFORE you say this is useless
there is a popular custom script that I think can use it right now
https://github.com/HerculesWS/Hercules/blob/master/npc/custom/quests/hunting_missions.txt#L75
although I was the one proposed that SQL method to Euphy, but I still think its ugly
if this function is merge, the script will be much cleaner