NOTE: this doesn't concern a special language, this is a general algorithm..



imagine a media player.. it has a list of songs to play.. and the user has it on shuffle mode.. so, each time it plays a song, that song shouldn't have been played before.. and so on..

for the sake of simplicity, let's denote the song by a number.. so let's say we have a list of numbers from 1 to 10.. the random generator will pick number 5, and we put that in a list, let's call it playedList.. so the next time the random generator picks up the number 3.. then, it picks number 5 again.. it checks to see if 5 existed before, so it checks playedList.. it was there? pick another number.. and so forth..

so far so good..
but.. this is a list of 10.. imagine at the 9th time, it gets a random number, and it has to check that number with playedList, which by now has 8 numbers.. it doesn't seem like much waste of time.. but what about a list of 100? 1000? a million?

we all know this, which is good.. and widely used..
so i was thinkin' to come up with some kind of a bit-mask checking.. let's say we have a bit-mask variable, call it bitMask.. everytime we generate a number that hasn't been used before, we OR it to bitMask..

here's a simple case scenario:

init
bitMask = 0000

first time
number 4 = 0100
0100 AND 0000 = 0000 != 0100
which means didn't exist before..
set bitMask = bitMask OR 0100 = 0100

second time
8 = 1000
0100 AND 1000 = 0000 != 1000
didn't exist.. execute it..
set bitMask = bitMask OR 1000 = 1100

SIDE-NOTE: now, in bitMask, both 4 and 8 existed before, altho it's represented by one number 1100; 12 in decimal..

third time
2 = 0010
0010 AND 1100 = 0000 != 0010
didn't exist.. execute it..
set bitMask = bitMask OR 0010 = 1110

forth time
4 = 0100
0100 AND 1110 = 0100 != 0100
existed before.. don't execute..
don't change bitMask..

fifth time
1.. simple enough..
bitMask = 1111

SIDE-NOTE: note that so far, bitMask 1111 denotes that the numbers 1, 2, 4, 8 existed before..

sixth time
assume it gets to be the number 15..
15 = 1111..
1111 AND 1111 = 1111 = 1111
this means that 15 existed before, altho we know, for sure, that it didn't..



so, it seemed to be working fine, until the sixth time took place.. do you think we could come up with a workaround?

i appreciate you reading up to THIS POINT..
thank you..