This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Is this C implementation of Fisher-Yates shuffle correct?
In order to simulate different order of input sequence into my application, I would like to generate a list of randomize sequence for an array input. For example, given an arr[10], the default sequence is 0,1,..,8,9. However, I'd like to manipulate the sequence into random order, for eg, 2,4,5,1,9,0,3,7,8,6.
I think rand() will generate a random value between 0-9, but it doesn't assure that each element is generated at least once. In such case, I am thinking about below pseudo, but is there a better way to generate random input sequence, and assure each number within the given range is generated at least once?
round #1:
generate a random number within 0-9.
let say 2 is selected
round #2:
generate a random number within 0-1
generate a random number within 3-9
let say 0 and 4 are selected
round #3:
generate a random number within 1
generate a random number within 3
generate a random number within 5-9
let say 1, 3, 7 are selected
round #4:
generate a random number within 5-6
generate a random number within 8-9
continue until 10 numbers are selected
Look at Fisher-Yates shuffle algorithm here for your requirement.
Here is another approach:
Create array of 10 elements and fill it with the values you want to randomize (0, 1, ..., 9).
Iterate for k from 9 down to 0 and pick a random number index = rand(k).
Swap the element at position k with the element at position index.
At the end your array will contain a random permutation of the original values, which is exactly what you wanted.
Related
Simple Question:
I want to generate random numbers from 1 to 15 including 15, but I keep getting duplicates in the resulting serie.
I want to be able to get a random order of every number from 1 to 15. In the Delphi programming language.
That (as mentioned in comments) is easily achieved using the Fisher-Yates algorithm:
TYPE TIntArr = TArray<Integer>;
FUNCTION RandomList(Min,Max : Integer) : TIntArr;
VAR
I,J,K : Integer;
BEGIN
SetLength(Result,SUCC(Max-Min));
FOR I:=Min TO Max DO Result[I-Min+LOW(Result)]:=I;
FOR I:=HIGH(Result) DOWNTO SUCC(LOW(Result)) DO BEGIN
J:=RANDOM(I);
K:=Result[I]; Result[I]:=Result[J]; Result[J]:=K
END
END;
The above function will give you a dynamic integer array containing a random order of numbers between Min and Max (both inclusive).
Remember to call RANDOMIZE in your main form's FormCreate to initialize the PRNG (Pseudo-Random Number Generator) to a fairly random value. If you don't, you'll get the same list returned every time...
How to make pairs of a sorted array such that the addition on the pairs after by multiplying of a pair is highest?
for example:
input array A[6,3,2,5]
output: (6 * 5)+(3 * 2)=36
You can achieve maximum product by multiplying the first two largest numbers. In the example you have given, A[6, 3, 2, 5]. The two largest number are 6 and 5. Therefore the maximum product that can be achieved by picking any two numbers from the array is 6*5 = 30.
I can help more if you may give more examples and show how to do the pairing in inputs of larger size. Describe your question a bit more.
For one of my homework problems, we had to write a function that creates an array containing n random numbers between 1 and 365. (Done). Then, check if any of these n birthdays are identical. Is there a shorter way to do this than doing several loops or several logical expressions?
Thank you!
CODE SO FAR, NOT DONE YET!!
function = [prob] bdayprob(N,n)
N = input('Please enter the number of experiments performed: N = ');
n = input('Please enter the sample size: n = ');
count = 0;
for(i=1:n)
x(i) = randi(365);
if(x(i)== x)
count = count + 1
end
return
If I'm interpreting your question properly, you want to check to see if generating n integers or days results in n unique numbers. Given your current knowledge in MATLAB, it's as simple as doing:
n = 30; %// Define sample size
N = 10; %// Define number of trials
%// Define logical array where each location tells you whether
%// birthdays were repeated for a trial
check = false(1, N);
%// For each trial...
for idx = 1 : N
%// Generate sample size random numbers
days = randi(365, n, 1);
%// Check to see if the total number of unique birthdays
%// are equal to the sample size
check(idx) = numel(unique(days)) == n;
end
Woah! Let's go through the code slowly shall we? We first define the sample size and the number of trials. We then specify a logical array where each location tells you whether or not there were repeated birthdays generated for that trial. Now, we start with a loop where for each trial, we generate random numbers from 1 to 365 that is of n or sample size long. We then use unique and figure out all unique integers that were generated from this random generation. If all of the birthdays are unique, then the total number of unique birthdays generated should equal the sample size. If we don't, then we have repeats. For example, if we generated a sample of [1 1 1 2 2], the output of unique would be [1 2], and the total number of unique elements is 2. Since this doesn't equal 5 or the sample size, then we know that the birthdays generated weren't unique. However, if we had [1 3 4 6 7], unique would give the same output, and since the output length is the same as the sample size, we know that all of the days are unique.
So, we check to see if this number is equal to the sample size for each iteration. If it is, then we output true. If not, we output false. When I run this code on my end, this is what I get for check. I set the sample size to 30 and the number of trials to be 10.
check =
0 0 1 1 0 0 0 0 1 0
Take note that if you increase the sample size, there is a higher probability that you will get duplicates, because randi can be considered as sampling with replacement. Therefore, the larger the sample size, the higher the chance of getting duplicate values. I made the sample size small on purpose so that we can see that it's possible to get unique days. However, if you set it to something like 100, or 200, you will most likely get check to be all false as there will most likely be duplicates per trial.
Here are some more approaches that avoid loops. Let
n = 20; %// define sample size
x = randi(365,n,1); %// generate n values between 1 and 365
Any of the following code snippets returns true (or 1) if there are two identical values in x, and false (or 0) otherwise:
Sort and then check if any two consecutive elements are the same:
result = any(diff(sort(x))==0);
Do all pairwise comparisons manually; remove self-pairs and duplicate pairs; and check if any of the remaining comparisons is true:
result = nnz(tril(bsxfun(#eq, x, x.'),-1))>0;
Compute the distance between distinct values, considering each pair just once, and then check if any distance is 0:
result = any(pdist(x(:))==0);
Find the number of occurrences of the most common value (mode):
[~, occurs] = mode(x);
result = occurs>1;
I don't know if I'm supposed to solve the problem for you, but perhaps a few hints may lead you in the right direction (besides I'm not a matlab expert so it will be in general terms):
Maybe not, but you have to ask yourself what they expect of you. The solution you propose requires you to loop through the array in two nested loops which will mean n*(n-1)/2 times through the loop (ie quadratic time complexity).
There are a number of ways you can improve the time complexity of the problem. The most straightforward would be to have a 365 element table where you can keep track if a particular number has been seen yet - which would require only a single loop (ie linear time complexity), but perhaps that's not what they're looking for either. But maybe that solution is a little bit ad-hoc? What we're basically looking for is a fast lookup if a particular number has been seen before - there exists more memory efficient structures that allows look up in O(1) time and O(log n) time (if you know these you have an arsenal of tools to use).
Then of course you could use the pidgeonhole principle to provide the answer much faster in some special cases (remember that you only asked to determine whether two or more numbers are equal or not).
In Ruby:
I sample element from array. I see duplicate (same element) every 30 samples or so. Sometimes as close as 5-6 samples apart. Why?
This is my code:
some_array = IO.readlines("file with 5000 unique elements")
some_array.shuffle!
#random_element = some_array.sample
puts #random_element
If you want n random non-duplicate elements from your array, you should call some_array.sample(n).
Sample doesn't guarantee that two consecutive calls will not contain duplicates; it guarantees that all elements chosen in one call won't.
I've written a basic permutation program in C.
The user types a number, and it prints all the permutations of that number.
Basically, this is how it works (the main algorithm is the one used to find the next higher permutation):
int currentPerm = toAscending(num);
int lastPerm = toDescending(num);
int counter = 1;
printf("%d", currentPerm);
while (currentPerm != lastPerm)
{
counter++;
currentPerm = nextHigherPerm(currentPerm);
printf("%d", currentPerm);
}
However, when the number input includes repeated digits - duplicates - some permutations are not being generated, since they're duplicates. The counter shows a different number than it's supposed to - Instead of showing the factorial of the number of digits in the number, it shows a smaller number, of only unique permutations.
For example:
num = 1234567
counter = 5040 (!7 - all unique)
num = 1123456
counter = 2520
num = 1112345
counter = 840
I want to it to treat repeated/duplicated digits as if they were different - I don't want to generate only unique permutations - but rather generate all the permutations, regardless of whether they're repeated and duplicates of others.
Uhm... why not just calculate the factorial of the length of the input string then? ;)
I want to it to treat repeated/duplicated digits as if they were
different - I don't want to calculate only the number of unique
permutations.
If the only information that nextHigherPerm() uses is the number that's passed in, you're out of luck. Consider nextHigherPerm(122). How can the function know how many versions of 122 it has already seen? Should nextHigherPerm(122) return 122 or 212? There's no way to know unless you keep track of the current state of the generator separately.
When you have 3 letters for example ABC, you can make: ABC, ACB, BAC, BCA, CAB, CBA, 6 combinations (6!). If 2 of those letters repeat like AAB, you can make: AAB, ABA, BAA, IT IS NOT 3! so What is it? From where does it comes from? The real way to calculate it when a digit or letter is repeated is with combinations -> ( n k ) = n! / ( n! * ( n! - k! ) )
Let's make another illustrative example: AAAB, then the possible combinations are AAAB, AABA, ABAA, BAAA only four combinations, and if you calcualte them by the formula 4C3 = 4.
How is the correct procedure to generate all these lists:
Store the digits in an array. Example ABCD.
Set the 0 element of the array as the pivot element, and exclude it from the temp array. A {BCD}
Then as you want all the combinations (Even the repeated), move the elements of the temporal array to the right or left (However you like) until you reach the n element.
A{BCD}------------A{CDB}------------A{DBC}
Do the second step again but with the temp array.
A{B{CD}}------------A{C{DB}}------------A{D{BC}}
Do the third step again but inside the second temp array.
A{B{CD}}------------A{C{DB}}------------A{D{BC}}
A{B{DC}}------------A{C{BD}}------------A{D{CB}}
Go to the first array and move the array, BCDA, set B as pivot, and do this until you find all combinations.
Why not convert it to a string then treat your program like an anagram generator?