+ 1

Can somebody help me understand this code?

I think I might understand it, but I still don't get some things. https://code.sololearn.com/cQQX9iMtPyHN/?ref=app This code was given to me by somebody trying to help me understand random generation.

26th Jan 2019, 5:10 PM
Daniel Cooper
Daniel Cooper - avatar
1 ответ
+ 1
The declaration of the sample function is so that the main knows it exists. This can be done outside of the main, or you can simply define sample before the main to not have to do it. You don't need a return in the sample function, since it's void. The sample function put the values 0 to 7 randomly in the array given. This is done by randomly picking an index that has no assigned value yet (the array of booleans keep track of the indexes with an assigned value) (while the random index has an assigned value, reroll), putting 0 at that index, then another index with no assigned value, putting 1 at that index, etc. Not too great since you have to reroll a lot for the later indexes. For a better way to shuffle an array, see the Fisher-Yates shuffle algorithm: https://en.m.wikipedia.org/wiki/Fisher–Yates_shuffle
26th Jan 2019, 5:53 PM
Zen
Zen - avatar