Back to Problem Solutions forum
I´m a bit puzzled. It is my understanding that a given random number is taken by modulo 52 if it is greater then 52,
if not I´ll leave it unchanged.
In the given example this will lead to following result:
SK should be put to position 19 (Which is also the case in the example).
SQ has an initial random number 8708, so I change it to n%52 which gives me as result position 24.
Now comes my problem, in the given example SQ located at index 47 after the shuffle, rather then the 24th.
In my understanding ST should have as final position index 4 (as random number given to this card is 4), but in the example
it is located at index 11.
I give you an idea how I tried to solve this problem:
1st I create an unshuffled deck.
2nd I create and deck to shuffle.
3rd I´m putting all the random numbers into an integer array.
4th I´m swapping the first card with the one given by the random number (In the example this would be CA swapped with S4)
5th I'm redoing steps 3-5 for every card with the next random numbers.
In my program this gives these results(just a few ones to give you an idea of my results):
CA swapped with S4(42), C2 with D4(16), C3 with S4(0),..., S3 with S5(5),..., ST with C3(4).
I hope you understand my problem, I actually suppose that I´ve misunderstood something in the problem description.
A number must be taken modulo 52 if it is greater than 51.
You just need to
1st: Create an unshuffled deck
2nd: Use the numbers in the input data to shuffle the deck as described in the problem description
Sorry guys, I really don´t get it. I did it like in the explanation:
"For example, we take the first card CA and generate random value 15 for it
- this means that card should be moved to position 15, where D3 was - and D3
should be moved back to position 0. Then we take C2 from the position 1 and
generate next random number 50 - so C2 is swapped with SQ. And so on."
Now this is my result for the test case:
C5 D5 DK C8 ST S3 C7 C4 H3 H6 CT C3 H8 C2 C6 CJ HT H9 HA SK CK D4 DQ D2 SQ S2
DT D8 S6 S4 D3 HJ HQ S7 SA DA H5 H2 HK CA D7 SJ S9 DJ S8 S5 C9 D9 CQ D6 H4 H7
And this is the expected result for the test case:
C5 D5 S4 C8 CQ S3 HK C9 H3 H6 D3 ST DT HT C6 CK DA H9 SJ SK DK C2 DQ S5 H4 D7
S7 S2 C4 D9 CT HJ HQ D2 SA CA H5 H2 C7 D4 CJ D6 S9 HA S8 D8 S6 SQ C3 DJ H8 H7
So you see there are some cards on the correct place but some are´nt, and I´m
a bit puzzled why?. The random shuffling position is an absolute one if I understand
it, rather then a relative one.
But I really can´t pinpoint my error.
I can see where you are getting "random value 15" and "random value 50" from, but ...
You should be starting with position 0, reading 5814 from the input data, taking that mod 52 which gives 42, then swapping positions 0 and 42.
At position 1, you read 1316, 1316 mod 52 is 16, so you swap positions 1 & 16.
At position 2, you read 2080, 2080 mod 52 is 0, so you swap positions 2 & 0.
Then continue with positions 3 to 51.
I hope that helps.
Ok thank you, I´ve just found my error. I was using the random numbers to the following card
of an unshuffled deck, instead of using it to the shuffling deck. I´ve got it now,
Thank you.