Path: csiph.com!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Mok-Kong Shen Newsgroups: comp.games.development.programming.algorithms,comp.games.development.programming.misc Subject: A practical alternative of the method of random perumtation of Fisher & Yates Date: Tue, 25 Jun 2013 10:33:36 +0200 Organization: albasani.net Lines: 148 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net t+cqbcqNalmnTIhtfOU/KFPK7PRijB1wKn6gn4eULZjEnycmqueO0PQbIGI7K5vk2dx3K/hkcCcK1hCNFIGQ1drnAS8bw2ohZ3G24vGjF+/XvpjbdhgfkUr4mFhQqTwP NNTP-Posting-Date: Tue, 25 Jun 2013 08:33:32 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="Dj8osUfp2IPIh5dgdzAdXJf0AAV10ioM9m3wnYv4S7DDQAOwyVBBedN851YhU3vs4+sZ7O+NvM5UDMUEdjgrU6mznqfjjPGR0EEwWZLdqhWmsIX37yrpL3h/W+MCmaTb"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 Cancel-Lock: sha1:2EXKrawCcYhj97BpcV8FgzN7b7k= Xref: csiph.com comp.games.development.programming.algorithms:44 comp.games.development.programming.misc:32 For performing (pseudo-)random permutations the algorithm of Fisher & Yates is theoretically perfect (i.e. disregarding the imperfectness of the PRNG involved) but requires for dealing with a list of n elments n-1 PRNs to generate one permutation. There are however cases in practice where a couple of PRNs are available from the beginning due to other reasons and thus it could be of some interest to see whether one couldn't simply make use of such PRNs for random permutations as well instead of specially acquiring the n-1 PRNs that are needed to do the F&Y processing. To avoid misunderstanding, it should be stated that the practical situation we'll treat is one where n is moderately large (albeit not too small) and that the number of random permutations to be generated, while substantial, is nonetheless small in comparison to the factorial of n, i.e. one doesn't need all, or almost all, of the possible permutations of n elements. Through experiments I found that the following function shuffle() (written in Python), which requires only 2 PRNs in each invocation, appears to be a viable candidate in the above mentioned context: # in-shuffle def riffle(cards,n): newcards=[] nh=n//2 for i in range(nh): newcards.append(cards[nh+i]) newcards.append(cards[i]) if nh*26 dupl. shuffle() 757779 104866 9872 678 30 2 0 0 F&Y n=10 759264 104475 9600 678 50 3 1 0 shuffle() 999303 349 0 0 0 0 0 0 F&Y n=15 1000001 0 0 0 0 0 0 0 shuffle() 999999 1 0 0 0 0 0 0 F&Y n=20 1000001 0 0 0 0 0 0 0 shuffle() 1000001 0 0 0 0 0 0 0 F&Y n=25 1000001 0 0 0 0 0 0 0 shuffle() 999979 11 0 0 0 0 0 0 F&Y n=30 1000001 0 0 0 0 0 0 0 shuffle() 1000001 0 0 0 0 0 0 0 F&Y n=40 1000001 0 0 0 0 0 0 0 shuffle() 1000001 0 0 0 0 0 0 0 F&Y n=50 1000001 0 0 0 0 0 0 0 shuffle() 1000001 0 0 0 0 0 0 0 F&Y n=60 1000001 0 0 0 0 0 0 0 Taking consideration also of the fact that the PRNGs used in practice are not perfect, the function shuffle() thus seems to have fairly well fulfilled its intended purpose. (The function has been used in a software of mine in http://s13.zetaboards.com/Crypto/topic/6925232/1/) M. K. Shen