Performing fisher - yates shuffle directly in a 2D array












1















I have been trying, with no success, to perform a partial fisher-yates shuffle directly for the first N elements of a 2D array. I know I could transform the 2D array into a 1D array, perform the shuffle and then turn it back, but if possible I would like to avoid this.



The problem in my implementation is, I think although I have not proved it, that the last elements of each row of my array are not correctly shuffled. In fact, supposingI have a m*n array,of which the first N are equal to 1 and the rest m*n - N are equal to 0. When I perform my shuffle of the first N elements of the array, about 67% of the time the elements at the end of each row: array[i][end] are equal to 1, which seems to me to be too many times.



Here is my implementation along with a driver code that can be run to show what the problem is:



void swap(int **a, int i, int j, int iNew, int jNew) 
{
int temp = a[i][j];
a[i][j] = a[iNew][jNew];
a[iNew][jNew] = temp;
}

void fisher_yates_shuffle(int n, int nbLines, int nbColumns, int **a)
{
for (int i = 0; i < nbLines; i++)
{
for (int j = 0; j < nbColumns; j++)
{
swap(a, i, j, i+rand()%(nbLines-i), j+rand()%(nbColumns -j)); // swap element with random later element

n--;

if(n<=0)
break;
}
if(n<=0)
break;
}
}

int main(int argc, char const *argv)
{
int count1 = 0, count2 = 0, count3 = 0, count4 = 0, N = 10, k = 100000;
srand(time(NULL));

//Short example of a 5 by 5 array, with N = 10 first elements equal to 1
//that need to to be shuffled among all of its elements.
while(k > 0)
{
//allocate
N = 10;
int **a = (int**)malloc(sizeof(int*) * 5);
for(int i = 0; i < 5; i++)
a[i] = (int*)malloc(sizeof(int) * 5);

//initialize
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
if(N > 0)
a[i][j] = 1;
else
a[i][j] = 0;
N--;
}
}

//shuffle
fisher_yates_shuffle(10, 5, 5, a);

//count how many times the last element of each row is equal to 1.
if(a[1][4] == 1)
count1++;
if(a[2][4] == 1)
count2++;
if(a[3][4] == 1)
count3++;
if(a[4][4] == 1)
count4++;

//destroy memory allocated.
for(int i = 0; i < 5; i++)
free(a[i]);
free(a);

k--;
}

//print approximate results.
printf("%d %d %d %dn", (count1 * 100)/100000, (count2 * 100)/100000, (count3 * 100)/100000, (count4 * 100/100000));

return 0;
}


I know it doesn't look very good and there must be a more efficient way of doing it. Maybe there is a different, equally efficient algorithm to shuffle the first N elements of a 2D array like that?










share|improve this question




















  • 2





    Apparently a is an array of pointers, but you should clarify that by editing the question. Also, it would help to see what the swap function does. In summary, this question needs a Minimal, Complete, and Verifiable example.

    – user3386109
    Nov 15 '18 at 23:34








  • 1





    Role of int n unclear. I'd expect n to be reset each for (int i = 0; i < nbLines; i++) iteration.

    – chux
    Nov 15 '18 at 23:45








  • 2





    Given the implementation in your snippet, you could just use a plain array of n*m elements. BTW, have you properly used srand, somewhere in your code?

    – Bob__
    Nov 15 '18 at 23:48








  • 1





    Instead of rand(), which produces randoms in range 0..RAND_MAX, and modulo operator, which does not provide even distribution, you should use better random function and better distribution, like those from <random> header, but for C instead for C++.

    – Dialecticus
    Nov 15 '18 at 23:50








  • 1





    @Dialecticus Given that the number of lines is 5 and that RAND_MAX is likely something like 2147483647, I'd bet quite a sum that the modulo operator doesn't introduce any measurable problems with the distribution.

    – Andrew Henle
    Nov 16 '18 at 0:06
















1















I have been trying, with no success, to perform a partial fisher-yates shuffle directly for the first N elements of a 2D array. I know I could transform the 2D array into a 1D array, perform the shuffle and then turn it back, but if possible I would like to avoid this.



The problem in my implementation is, I think although I have not proved it, that the last elements of each row of my array are not correctly shuffled. In fact, supposingI have a m*n array,of which the first N are equal to 1 and the rest m*n - N are equal to 0. When I perform my shuffle of the first N elements of the array, about 67% of the time the elements at the end of each row: array[i][end] are equal to 1, which seems to me to be too many times.



Here is my implementation along with a driver code that can be run to show what the problem is:



void swap(int **a, int i, int j, int iNew, int jNew) 
{
int temp = a[i][j];
a[i][j] = a[iNew][jNew];
a[iNew][jNew] = temp;
}

void fisher_yates_shuffle(int n, int nbLines, int nbColumns, int **a)
{
for (int i = 0; i < nbLines; i++)
{
for (int j = 0; j < nbColumns; j++)
{
swap(a, i, j, i+rand()%(nbLines-i), j+rand()%(nbColumns -j)); // swap element with random later element

n--;

if(n<=0)
break;
}
if(n<=0)
break;
}
}

int main(int argc, char const *argv)
{
int count1 = 0, count2 = 0, count3 = 0, count4 = 0, N = 10, k = 100000;
srand(time(NULL));

//Short example of a 5 by 5 array, with N = 10 first elements equal to 1
//that need to to be shuffled among all of its elements.
while(k > 0)
{
//allocate
N = 10;
int **a = (int**)malloc(sizeof(int*) * 5);
for(int i = 0; i < 5; i++)
a[i] = (int*)malloc(sizeof(int) * 5);

//initialize
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
if(N > 0)
a[i][j] = 1;
else
a[i][j] = 0;
N--;
}
}

//shuffle
fisher_yates_shuffle(10, 5, 5, a);

//count how many times the last element of each row is equal to 1.
if(a[1][4] == 1)
count1++;
if(a[2][4] == 1)
count2++;
if(a[3][4] == 1)
count3++;
if(a[4][4] == 1)
count4++;

//destroy memory allocated.
for(int i = 0; i < 5; i++)
free(a[i]);
free(a);

k--;
}

//print approximate results.
printf("%d %d %d %dn", (count1 * 100)/100000, (count2 * 100)/100000, (count3 * 100)/100000, (count4 * 100/100000));

return 0;
}


I know it doesn't look very good and there must be a more efficient way of doing it. Maybe there is a different, equally efficient algorithm to shuffle the first N elements of a 2D array like that?










share|improve this question




















  • 2





    Apparently a is an array of pointers, but you should clarify that by editing the question. Also, it would help to see what the swap function does. In summary, this question needs a Minimal, Complete, and Verifiable example.

    – user3386109
    Nov 15 '18 at 23:34








  • 1





    Role of int n unclear. I'd expect n to be reset each for (int i = 0; i < nbLines; i++) iteration.

    – chux
    Nov 15 '18 at 23:45








  • 2





    Given the implementation in your snippet, you could just use a plain array of n*m elements. BTW, have you properly used srand, somewhere in your code?

    – Bob__
    Nov 15 '18 at 23:48








  • 1





    Instead of rand(), which produces randoms in range 0..RAND_MAX, and modulo operator, which does not provide even distribution, you should use better random function and better distribution, like those from <random> header, but for C instead for C++.

    – Dialecticus
    Nov 15 '18 at 23:50








  • 1





    @Dialecticus Given that the number of lines is 5 and that RAND_MAX is likely something like 2147483647, I'd bet quite a sum that the modulo operator doesn't introduce any measurable problems with the distribution.

    – Andrew Henle
    Nov 16 '18 at 0:06














1












1








1








I have been trying, with no success, to perform a partial fisher-yates shuffle directly for the first N elements of a 2D array. I know I could transform the 2D array into a 1D array, perform the shuffle and then turn it back, but if possible I would like to avoid this.



The problem in my implementation is, I think although I have not proved it, that the last elements of each row of my array are not correctly shuffled. In fact, supposingI have a m*n array,of which the first N are equal to 1 and the rest m*n - N are equal to 0. When I perform my shuffle of the first N elements of the array, about 67% of the time the elements at the end of each row: array[i][end] are equal to 1, which seems to me to be too many times.



Here is my implementation along with a driver code that can be run to show what the problem is:



void swap(int **a, int i, int j, int iNew, int jNew) 
{
int temp = a[i][j];
a[i][j] = a[iNew][jNew];
a[iNew][jNew] = temp;
}

void fisher_yates_shuffle(int n, int nbLines, int nbColumns, int **a)
{
for (int i = 0; i < nbLines; i++)
{
for (int j = 0; j < nbColumns; j++)
{
swap(a, i, j, i+rand()%(nbLines-i), j+rand()%(nbColumns -j)); // swap element with random later element

n--;

if(n<=0)
break;
}
if(n<=0)
break;
}
}

int main(int argc, char const *argv)
{
int count1 = 0, count2 = 0, count3 = 0, count4 = 0, N = 10, k = 100000;
srand(time(NULL));

//Short example of a 5 by 5 array, with N = 10 first elements equal to 1
//that need to to be shuffled among all of its elements.
while(k > 0)
{
//allocate
N = 10;
int **a = (int**)malloc(sizeof(int*) * 5);
for(int i = 0; i < 5; i++)
a[i] = (int*)malloc(sizeof(int) * 5);

//initialize
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
if(N > 0)
a[i][j] = 1;
else
a[i][j] = 0;
N--;
}
}

//shuffle
fisher_yates_shuffle(10, 5, 5, a);

//count how many times the last element of each row is equal to 1.
if(a[1][4] == 1)
count1++;
if(a[2][4] == 1)
count2++;
if(a[3][4] == 1)
count3++;
if(a[4][4] == 1)
count4++;

//destroy memory allocated.
for(int i = 0; i < 5; i++)
free(a[i]);
free(a);

k--;
}

//print approximate results.
printf("%d %d %d %dn", (count1 * 100)/100000, (count2 * 100)/100000, (count3 * 100)/100000, (count4 * 100/100000));

return 0;
}


I know it doesn't look very good and there must be a more efficient way of doing it. Maybe there is a different, equally efficient algorithm to shuffle the first N elements of a 2D array like that?










share|improve this question
















I have been trying, with no success, to perform a partial fisher-yates shuffle directly for the first N elements of a 2D array. I know I could transform the 2D array into a 1D array, perform the shuffle and then turn it back, but if possible I would like to avoid this.



The problem in my implementation is, I think although I have not proved it, that the last elements of each row of my array are not correctly shuffled. In fact, supposingI have a m*n array,of which the first N are equal to 1 and the rest m*n - N are equal to 0. When I perform my shuffle of the first N elements of the array, about 67% of the time the elements at the end of each row: array[i][end] are equal to 1, which seems to me to be too many times.



Here is my implementation along with a driver code that can be run to show what the problem is:



void swap(int **a, int i, int j, int iNew, int jNew) 
{
int temp = a[i][j];
a[i][j] = a[iNew][jNew];
a[iNew][jNew] = temp;
}

void fisher_yates_shuffle(int n, int nbLines, int nbColumns, int **a)
{
for (int i = 0; i < nbLines; i++)
{
for (int j = 0; j < nbColumns; j++)
{
swap(a, i, j, i+rand()%(nbLines-i), j+rand()%(nbColumns -j)); // swap element with random later element

n--;

if(n<=0)
break;
}
if(n<=0)
break;
}
}

int main(int argc, char const *argv)
{
int count1 = 0, count2 = 0, count3 = 0, count4 = 0, N = 10, k = 100000;
srand(time(NULL));

//Short example of a 5 by 5 array, with N = 10 first elements equal to 1
//that need to to be shuffled among all of its elements.
while(k > 0)
{
//allocate
N = 10;
int **a = (int**)malloc(sizeof(int*) * 5);
for(int i = 0; i < 5; i++)
a[i] = (int*)malloc(sizeof(int) * 5);

//initialize
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
if(N > 0)
a[i][j] = 1;
else
a[i][j] = 0;
N--;
}
}

//shuffle
fisher_yates_shuffle(10, 5, 5, a);

//count how many times the last element of each row is equal to 1.
if(a[1][4] == 1)
count1++;
if(a[2][4] == 1)
count2++;
if(a[3][4] == 1)
count3++;
if(a[4][4] == 1)
count4++;

//destroy memory allocated.
for(int i = 0; i < 5; i++)
free(a[i]);
free(a);

k--;
}

//print approximate results.
printf("%d %d %d %dn", (count1 * 100)/100000, (count2 * 100)/100000, (count3 * 100)/100000, (count4 * 100/100000));

return 0;
}


I know it doesn't look very good and there must be a more efficient way of doing it. Maybe there is a different, equally efficient algorithm to shuffle the first N elements of a 2D array like that?







c arrays algorithm random shuffle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 0:47







Desperados

















asked Nov 15 '18 at 23:25









DesperadosDesperados

565




565








  • 2





    Apparently a is an array of pointers, but you should clarify that by editing the question. Also, it would help to see what the swap function does. In summary, this question needs a Minimal, Complete, and Verifiable example.

    – user3386109
    Nov 15 '18 at 23:34








  • 1





    Role of int n unclear. I'd expect n to be reset each for (int i = 0; i < nbLines; i++) iteration.

    – chux
    Nov 15 '18 at 23:45








  • 2





    Given the implementation in your snippet, you could just use a plain array of n*m elements. BTW, have you properly used srand, somewhere in your code?

    – Bob__
    Nov 15 '18 at 23:48








  • 1





    Instead of rand(), which produces randoms in range 0..RAND_MAX, and modulo operator, which does not provide even distribution, you should use better random function and better distribution, like those from <random> header, but for C instead for C++.

    – Dialecticus
    Nov 15 '18 at 23:50








  • 1





    @Dialecticus Given that the number of lines is 5 and that RAND_MAX is likely something like 2147483647, I'd bet quite a sum that the modulo operator doesn't introduce any measurable problems with the distribution.

    – Andrew Henle
    Nov 16 '18 at 0:06














  • 2





    Apparently a is an array of pointers, but you should clarify that by editing the question. Also, it would help to see what the swap function does. In summary, this question needs a Minimal, Complete, and Verifiable example.

    – user3386109
    Nov 15 '18 at 23:34








  • 1





    Role of int n unclear. I'd expect n to be reset each for (int i = 0; i < nbLines; i++) iteration.

    – chux
    Nov 15 '18 at 23:45








  • 2





    Given the implementation in your snippet, you could just use a plain array of n*m elements. BTW, have you properly used srand, somewhere in your code?

    – Bob__
    Nov 15 '18 at 23:48








  • 1





    Instead of rand(), which produces randoms in range 0..RAND_MAX, and modulo operator, which does not provide even distribution, you should use better random function and better distribution, like those from <random> header, but for C instead for C++.

    – Dialecticus
    Nov 15 '18 at 23:50








  • 1





    @Dialecticus Given that the number of lines is 5 and that RAND_MAX is likely something like 2147483647, I'd bet quite a sum that the modulo operator doesn't introduce any measurable problems with the distribution.

    – Andrew Henle
    Nov 16 '18 at 0:06








2




2





Apparently a is an array of pointers, but you should clarify that by editing the question. Also, it would help to see what the swap function does. In summary, this question needs a Minimal, Complete, and Verifiable example.

– user3386109
Nov 15 '18 at 23:34







Apparently a is an array of pointers, but you should clarify that by editing the question. Also, it would help to see what the swap function does. In summary, this question needs a Minimal, Complete, and Verifiable example.

– user3386109
Nov 15 '18 at 23:34






1




1





Role of int n unclear. I'd expect n to be reset each for (int i = 0; i < nbLines; i++) iteration.

– chux
Nov 15 '18 at 23:45







Role of int n unclear. I'd expect n to be reset each for (int i = 0; i < nbLines; i++) iteration.

– chux
Nov 15 '18 at 23:45






2




2





Given the implementation in your snippet, you could just use a plain array of n*m elements. BTW, have you properly used srand, somewhere in your code?

– Bob__
Nov 15 '18 at 23:48







Given the implementation in your snippet, you could just use a plain array of n*m elements. BTW, have you properly used srand, somewhere in your code?

– Bob__
Nov 15 '18 at 23:48






1




1





Instead of rand(), which produces randoms in range 0..RAND_MAX, and modulo operator, which does not provide even distribution, you should use better random function and better distribution, like those from <random> header, but for C instead for C++.

– Dialecticus
Nov 15 '18 at 23:50







Instead of rand(), which produces randoms in range 0..RAND_MAX, and modulo operator, which does not provide even distribution, you should use better random function and better distribution, like those from <random> header, but for C instead for C++.

– Dialecticus
Nov 15 '18 at 23:50






1




1





@Dialecticus Given that the number of lines is 5 and that RAND_MAX is likely something like 2147483647, I'd bet quite a sum that the modulo operator doesn't introduce any measurable problems with the distribution.

– Andrew Henle
Nov 16 '18 at 0:06





@Dialecticus Given that the number of lines is 5 and that RAND_MAX is likely something like 2147483647, I'd bet quite a sum that the modulo operator doesn't introduce any measurable problems with the distribution.

– Andrew Henle
Nov 16 '18 at 0:06












1 Answer
1






active

oldest

votes


















3














Expanding my previous comment, you could shuffle your "array" in a single loop:



void fisher_yates_shuffle(int n, int nbLines, int nbColumns, int **a) 
{
for (int i = 0; i < n; i++)
{
int j = i + rand() % (nbLines * nbColumns - i);
swap(a, i / nbColumns, i % nbColumns, j / nbColumns, j % nbColumns);
}
}


Note that there are far better ways to implement and pass around a matrix in C and that your swap function should return void, not int.






share|improve this answer


























  • This works perfectly, random positions get values of 1 with more or less the same probability. My "array" shuffle works now, thanks. I would also appreciate it if you could explain in more detail your comment about how I could pass my matrix into my function more efficiently

    – Desperados
    Nov 16 '18 at 0:46






  • 2





    @Desperados See e.g. stackoverflow.com/questions/42094465/… It's a long Q&A, but it's worth reading.

    – Bob__
    Nov 16 '18 at 0:49








  • 1





    I think indexes (i and j) have to be divides by nbColumns so that the algorithm may be applicable to 'arrays' that are not square (n by n). I think my observation is correct, maybe you want to consider editing your answer or explaining to me if I am wrong.

    – Desperados
    Nov 21 '18 at 4:45











  • @Desperados Yes, you are correct. Good catch, thanks.

    – Bob__
    Nov 21 '18 at 7:12











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329280%2fperforming-fisher-yates-shuffle-directly-in-a-2d-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Expanding my previous comment, you could shuffle your "array" in a single loop:



void fisher_yates_shuffle(int n, int nbLines, int nbColumns, int **a) 
{
for (int i = 0; i < n; i++)
{
int j = i + rand() % (nbLines * nbColumns - i);
swap(a, i / nbColumns, i % nbColumns, j / nbColumns, j % nbColumns);
}
}


Note that there are far better ways to implement and pass around a matrix in C and that your swap function should return void, not int.






share|improve this answer


























  • This works perfectly, random positions get values of 1 with more or less the same probability. My "array" shuffle works now, thanks. I would also appreciate it if you could explain in more detail your comment about how I could pass my matrix into my function more efficiently

    – Desperados
    Nov 16 '18 at 0:46






  • 2





    @Desperados See e.g. stackoverflow.com/questions/42094465/… It's a long Q&A, but it's worth reading.

    – Bob__
    Nov 16 '18 at 0:49








  • 1





    I think indexes (i and j) have to be divides by nbColumns so that the algorithm may be applicable to 'arrays' that are not square (n by n). I think my observation is correct, maybe you want to consider editing your answer or explaining to me if I am wrong.

    – Desperados
    Nov 21 '18 at 4:45











  • @Desperados Yes, you are correct. Good catch, thanks.

    – Bob__
    Nov 21 '18 at 7:12
















3














Expanding my previous comment, you could shuffle your "array" in a single loop:



void fisher_yates_shuffle(int n, int nbLines, int nbColumns, int **a) 
{
for (int i = 0; i < n; i++)
{
int j = i + rand() % (nbLines * nbColumns - i);
swap(a, i / nbColumns, i % nbColumns, j / nbColumns, j % nbColumns);
}
}


Note that there are far better ways to implement and pass around a matrix in C and that your swap function should return void, not int.






share|improve this answer


























  • This works perfectly, random positions get values of 1 with more or less the same probability. My "array" shuffle works now, thanks. I would also appreciate it if you could explain in more detail your comment about how I could pass my matrix into my function more efficiently

    – Desperados
    Nov 16 '18 at 0:46






  • 2





    @Desperados See e.g. stackoverflow.com/questions/42094465/… It's a long Q&A, but it's worth reading.

    – Bob__
    Nov 16 '18 at 0:49








  • 1





    I think indexes (i and j) have to be divides by nbColumns so that the algorithm may be applicable to 'arrays' that are not square (n by n). I think my observation is correct, maybe you want to consider editing your answer or explaining to me if I am wrong.

    – Desperados
    Nov 21 '18 at 4:45











  • @Desperados Yes, you are correct. Good catch, thanks.

    – Bob__
    Nov 21 '18 at 7:12














3












3








3







Expanding my previous comment, you could shuffle your "array" in a single loop:



void fisher_yates_shuffle(int n, int nbLines, int nbColumns, int **a) 
{
for (int i = 0; i < n; i++)
{
int j = i + rand() % (nbLines * nbColumns - i);
swap(a, i / nbColumns, i % nbColumns, j / nbColumns, j % nbColumns);
}
}


Note that there are far better ways to implement and pass around a matrix in C and that your swap function should return void, not int.






share|improve this answer















Expanding my previous comment, you could shuffle your "array" in a single loop:



void fisher_yates_shuffle(int n, int nbLines, int nbColumns, int **a) 
{
for (int i = 0; i < n; i++)
{
int j = i + rand() % (nbLines * nbColumns - i);
swap(a, i / nbColumns, i % nbColumns, j / nbColumns, j % nbColumns);
}
}


Note that there are far better ways to implement and pass around a matrix in C and that your swap function should return void, not int.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 7:10

























answered Nov 16 '18 at 0:36









Bob__Bob__

5,11331527




5,11331527













  • This works perfectly, random positions get values of 1 with more or less the same probability. My "array" shuffle works now, thanks. I would also appreciate it if you could explain in more detail your comment about how I could pass my matrix into my function more efficiently

    – Desperados
    Nov 16 '18 at 0:46






  • 2





    @Desperados See e.g. stackoverflow.com/questions/42094465/… It's a long Q&A, but it's worth reading.

    – Bob__
    Nov 16 '18 at 0:49








  • 1





    I think indexes (i and j) have to be divides by nbColumns so that the algorithm may be applicable to 'arrays' that are not square (n by n). I think my observation is correct, maybe you want to consider editing your answer or explaining to me if I am wrong.

    – Desperados
    Nov 21 '18 at 4:45











  • @Desperados Yes, you are correct. Good catch, thanks.

    – Bob__
    Nov 21 '18 at 7:12



















  • This works perfectly, random positions get values of 1 with more or less the same probability. My "array" shuffle works now, thanks. I would also appreciate it if you could explain in more detail your comment about how I could pass my matrix into my function more efficiently

    – Desperados
    Nov 16 '18 at 0:46






  • 2





    @Desperados See e.g. stackoverflow.com/questions/42094465/… It's a long Q&A, but it's worth reading.

    – Bob__
    Nov 16 '18 at 0:49








  • 1





    I think indexes (i and j) have to be divides by nbColumns so that the algorithm may be applicable to 'arrays' that are not square (n by n). I think my observation is correct, maybe you want to consider editing your answer or explaining to me if I am wrong.

    – Desperados
    Nov 21 '18 at 4:45











  • @Desperados Yes, you are correct. Good catch, thanks.

    – Bob__
    Nov 21 '18 at 7:12

















This works perfectly, random positions get values of 1 with more or less the same probability. My "array" shuffle works now, thanks. I would also appreciate it if you could explain in more detail your comment about how I could pass my matrix into my function more efficiently

– Desperados
Nov 16 '18 at 0:46





This works perfectly, random positions get values of 1 with more or less the same probability. My "array" shuffle works now, thanks. I would also appreciate it if you could explain in more detail your comment about how I could pass my matrix into my function more efficiently

– Desperados
Nov 16 '18 at 0:46




2




2





@Desperados See e.g. stackoverflow.com/questions/42094465/… It's a long Q&A, but it's worth reading.

– Bob__
Nov 16 '18 at 0:49







@Desperados See e.g. stackoverflow.com/questions/42094465/… It's a long Q&A, but it's worth reading.

– Bob__
Nov 16 '18 at 0:49






1




1





I think indexes (i and j) have to be divides by nbColumns so that the algorithm may be applicable to 'arrays' that are not square (n by n). I think my observation is correct, maybe you want to consider editing your answer or explaining to me if I am wrong.

– Desperados
Nov 21 '18 at 4:45





I think indexes (i and j) have to be divides by nbColumns so that the algorithm may be applicable to 'arrays' that are not square (n by n). I think my observation is correct, maybe you want to consider editing your answer or explaining to me if I am wrong.

– Desperados
Nov 21 '18 at 4:45













@Desperados Yes, you are correct. Good catch, thanks.

– Bob__
Nov 21 '18 at 7:12





@Desperados Yes, you are correct. Good catch, thanks.

– Bob__
Nov 21 '18 at 7:12




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329280%2fperforming-fisher-yates-shuffle-directly-in-a-2d-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values