How do I create an array of randomized pairs of numbers?
2 ビュー (過去 30 日間)
古いコメントを表示
So I am working on creating a version of the memory card game where you have say a 4x4 array of tiles, and each one has a matching pair within the array. I am using 8 different colors and 2 tiles for each color. How could I create a random 4x4 array that would only contain the numbers 1 through 8 but each one only 2 times? I assume I'd use randi. I know my title is quite confusing but I couldn't think of an easier description without telling why I needed it. Thank you!
0 件のコメント
採用された回答
Walter Roberson
2018 年 3 月 4 日
編集済み: Walter Roberson
2018 年 3 月 4 日
tiles = [1:8, 1:8];
tiles = reshape( tiles(randperm(length(tiles))), 4, 4);
3 件のコメント
Prabha Kumaresan
2018 年 3 月 5 日
it doesnt give me the result which i actually need Say for example if there are 30 users needs to be grouped such that each group should contain only 2 users and users present in one group should not be present in another group.
Walter Roberson
2018 年 3 月 5 日
Prabha Kumaresan:
Numusers = 30;
group_numbers = [1:Numusers/2, 1:Numusers/2];
group_for_user = group_numbers( randperm(NumUsers) );
Each group number will be used exactly twice. group_for_user will be a vector of which group number each user belongs to, and it is impossible for any user to be present in multiple groups because there is only one group number allocated for each user.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!