How to generate random number each time?

How would I efficiently generate a random permutation?

  • Assume that I have a not-so-efficient standard uniform random number generator. I want an efficient algorithm, that would generate any given permutation with probability ~1/N!. (approximately is good enough). In some schemes, you would need to justify why the probability is of any given permutation is gonna be about 1/N!. Since the random number generator may not be very efficient, do post "time complexity" analysis in terms of both (a) number of calls to random number generator (b) number of other elementary operations

  • Answer:

    (Posting my comment as an answer) The Fisher-Yates Shuffle guarantees that each permutation is generated with equal probability. Quoting Wikipedia (http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle): To shuffle an array a of n elements (indices 0..n-1):   for i from n − 1 downto 1 do       j ← random integer with 0 ≤ j ≤ i      exchange a[j] and a[i]

Shrey Banga at Quora Visit the source

Was this solution helpful to you?

Other answers

First, I am assuming that we need a random permutation of N numbers 0 to N-1. If not, then we can probably map the problem to such a form. I saw a brilliant algorithm for doing exactly this in the book Programming Pearls by Jon Bentley which in turn is based on Knuth's Algorithm P in section 3.4.2. The pseudocode is surprisingly simple - Given an array having integers from 0 to n-1 for i = [0, n)     swap(i, randint(i, n-1)) In C++, this would look something like int *x = new int[n]; // Initialize the array for(int i = 0; i < n; ++i) { x[i] = i; } for(int i = 0; i < n; ++i) { int j = randint(i, n-1); int temp = x[i]; x[i] = x[j]; x[j] = temp; } Edit: As mentioned above, this is equivalent to Fisher-Yates Shuffle and is also known as the Knuth Shuffle.

Natansh Verma

If you wanna know about the algorithm take a look at Fisher-Yates Shuffle. Following is the code in python, from itertools import permutations for string in permutations("abcde"): print string #or do what ever. eg:Appending It gives, ('a', 'b', 'c', 'd') ('a', 'b', 'd', 'c') ('a', 'c', 'b', 'd') ('a', 'c', 'd', 'b') ('a', 'd', 'b', 'c') ('a', 'd', 'c', 'b') ('b', 'a', 'c', 'd') ('b', 'a', 'd', 'c') ('b', 'c', 'a', 'd') ('b', 'c', 'd', 'a') ('b', 'd', 'a', 'c') ('b', 'd', 'c', 'a') ('c', 'a', 'b', 'd') ('c', 'a', 'd', 'b') ('c', 'b', 'a', 'd') ('c', 'b', 'd', 'a') ('c', 'd', 'a', 'b') ('c', 'd', 'b', 'a') ('d', 'a', 'b', 'c') ('d', 'a', 'c', 'b') ('d', 'b', 'a', 'c') ('d', 'b', 'c', 'a') ('d', 'c', 'a', 'b') ('d', 'c', 'b', 'a')

Sai Kiran

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.