If I had a 2d dynamic array (8x8) with 5 2's and 10 1's and all the rest 0's. How would I shuffle it, so all the numbers would be randomly placed on the grid? (The array has already been filled with these numbers)
Asked
Active
Viewed 190 times
-2
-
1I told you in [your previous question](http://stackoverflow.com/q/41857117/33732) that you should use a one-dimensional array. Shuffle it, and then put each item into the multidimensional array. – Rob Kennedy Jan 26 '17 at 16:27
-
1Possible duplicate of [How to generate a certain amount of numbers and spread them randomly across a grid?](http://stackoverflow.com/questions/41857117/how-to-generate-a-certain-amount-of-numbers-and-spread-them-randomly-across-a-gr) – GuidoG Jan 26 '17 at 16:33
1 Answers
2
Don't use a multi-dimensional array. Use a one-dimensional array of length 8*8 = 64. Place into that array your values. Then shuffle. Finally, pull the values off, one by one, into the multidimensional array. In pseudo code that looks like this:
N := 8;
SetLength(arr, N*N);
idx := 0;
for i := 1 to 5 do begin
arr[idx] := 2;
inc(idx);
end;
// etc., until array is populated
ShuffleArray(arr); // you know how to do this from your previous question
for idx := 0 to high(arr) do begin
Grid[idx div N, idx mod N] := arr[idx];
end;

David Heffernan
- 601,492
- 42
- 1,072
- 1,490
-
This didn't shuffle the list. It only displayed the 2's and 1's going down the y and x axis. https://gyazo.com/9086bb2813a799766817b5076b93dac2 – ban can Jan 26 '17 at 16:53
-
You didn't shuffle the array. Why didn't you shuffle the array. We've already told you how to do that. Don't make us do it over and over again. – David Heffernan Jan 26 '17 at 16:54
-
No. That's the part where you pull the shuffled values from the 1D array to a 2D grid. Don't just copy code blindly. Think. Read the code above again. Look more closely for the part that shuffles. The part that you ignored previously. See if you can see anything in the pseudo code that looks like it might shuffle. Please try to think. Getting us to do everything for you is not helpful to you. Don't you want to learn? – David Heffernan Jan 26 '17 at 16:57
-
I literally have done everything there. Populated the array. Don't tell me that its the ShuffleArray(arr) part because that's not even an identifier. I must be severely blind or something... – ban can Jan 26 '17 at 17:00
-
Well done, you have to actually shuffle the array. But we don't need to tell you how to do that because we already did so at the last question. – David Heffernan Jan 26 '17 at 17:01
-
Perhaps you should hire a programmer. And if you could be bothered to follow the links, you'd find Delphi code, which I presume is what you want. It never ceases to amaze me that people seem quite happy to blindly copy code without understanding it. Shuffling isn't hard. You just need to think a little. – David Heffernan Jan 26 '17 at 17:03
-
The issue isn't learning a language. I can implement Fisher-Yates in any language because I understand the algorithm. That's what you should be thinking about. Understand the algorithm. Then you've learnt something really useful. The link I gave you at your previous question has loads of good stuff. It has code to illustrate the algorithm. And links to lots of good articles that cover the algorithm and various examples of what happens when your screw it up. I'm sure you can do it if you are prepared to think. – David Heffernan Jan 26 '17 at 17:10
-
Figured it out. Didn't need to use fisher yates, but whatever. Thanks for the help. – ban can Jan 26 '17 at 17:37
-
That proves nothing. How can you be sure you sample from uniform random? – David Heffernan Jan 26 '17 at 17:43
-
2It always amazes me how people come here for help, get proper help, but not every bit or byte laid out for them, and then assume that tone of entitlement. Sheesh. @bancan: I guess you are in school to learn something. You had the opportunity, but missed it and got narky instead. Good show, really, and it will really make people jump through hoops to help you the next time. – Rudy Velthuis Jan 27 '17 at 13:45