Folgen

Is there a "shuffle-mapping" function? I need a bijective function to map each array position to a different, random-looking position.

· Web · 2 · 0 · 0

@anathem I usually create an array of indexes. You can shuffle this by 'common' means. If you find a better way, please let me know!

@TheConstructor Yeah, that's a bit of a problem in memory-restricted environments...

@anathem if you have an array with a length of 2^n you could try xor-ing with a random number. It will look random… If the length is not so well aligned you may be able to succeed with a shorter bit-mask (lower-bits 0), but will lose some 'random'-ness especially within groups. Or you could use a second approach for the after 2^n items

@anathem on second thought it's probably high-bits zero… write tests, please 😉

@anathem hm.. depends on the context and restrictions. maybe something like hash(index) % max_index could do.

@joknopp I don't think that would hold to be bijective ... I just need a way of running through the indices 0..n in a random-looking order. Ideally as a function, so that I don't need a mapping array.

@anathem oh, sorry, missed the bijective Part. That's trickier.

Melde dich an, um an der Konversation teilzuhaben
MastoKIF

Mastodon ist ein soziales Netzwerk. Es basiert auf offenen Web-Protokollen und freier, quelloffener Software. Es ist dezentral (so wie E-Mail!).