Just trying to shuffle the entries in an Exec double-linked list and was wondering if anyone has any handy code I could use before I start delving into something complicated.
void ShuffleList (struct List *source,struct List *dest){
struct Node *node;
int32 cnt;
cnt = CountNodes(source);
while(cnt >0){
node = GetNthNode(source,rand()% cnt);
IExec->Remove(node);
IExec->AddTail(dest, node);
cnt--;
}
}
The above code which I quickly put together uses the rand() clib function for random number generation so you may want to seed it first using srand(time(NULL)) f.e. or replace with your own code using UNIT_ENTROPY of timer.device f.e..
If you want to do an in place shuffle then you can do it like this:
Thanks Salass00, that's the way I ended up doing in the end. I just thought there would be a different method involving changing the pointers etc. but this seems to be the safest route.
The above code which I quickly put together uses the rand() clib function for random number generation so you may want to seed it first using srand(time(NULL)) f.e. or replace with your own code using UNIT_ENTROPY of timer.device f.e..
If you want to do an in place shuffle then you can do it like this:
Thanks Fredrik, very useful!
AmigaOne X5000-020 / 2GB RAM / Sapphire Pulse Radeon RX 560 / AmigaOS 4.1 Final Edition Update 2
Thanks Salass00, that's the way I ended up doing in the end. I just thought there would be a different method involving changing the pointers etc. but this seems to be the safest route.
Thanks again!
This won't work.
Use this instead: