-
-
Notifications
You must be signed in to change notification settings - Fork 560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The Array sort is not very random. #11
Comments
This is a better alternative to array.sort() Fisher-Yates Algorithmauthor : nitinpatel_20236
|
Hello. I am not sure it does place the cards next to each other after the sort? It seems to do it randomly for me? I will try out your algo too. Thanks for sharing! It looks super interesting |
In your example the array of cards is already in a random order. My array has the pairs of cards next to each other. That is what made it more obvious. The fisher-Yates algorithm might not be appropriate for this stage of teaching project. I just wanted to share it in case anybody else ran into the same problem. |
Thanks but i think there is some confusion. I am not sure why you are seeing pairs of cards next to each other as the array should be different each time. Math.random returns a random number between 0 and <1. So if it happens to give you a number less then 0.5 then you get a negative number and if it’s over that then you get a positive. So you are essentially sorting the array by assigning each item a value, and comparing one number to another , and shuffling or technically, 'sorting' the array that way. Like the sort algorithm does. https://forum.freecodecamp.org/t/how-does-math-random-work-to-sort-an-array/151540 https://teamtreehouse.com/community/return-mathrandom05 For example. If you do this: The array is different every time. We are simply using the same thing :) Do you mean the level of randomness? If is is the level of randomness then you are probably right. But for this project, for me 'random' was enough. |
Thanks for trying to reproduce my bug. I think I just got several lucky rolls because it is shuffling better now. I totally agree, the built-in is random enough, the point of this project is to learn how to write loops and functions and manipulate the DOM, not to ponder theories of randomness. Adding more cards might also help. It was interesting for me to find another way to approach it (generating a random sort) and at the time I thought I would be helping somebody with the same problem who might have been doubting themselves. |
If you feed the images into the card array in order,
{ name: 'ice-cream', img: 'images/ice-cream.png' }, { name: 'ice-cream', img: 'images/ice-cream.png' },
The array.sort seems to place the cards next to each other on the game board.
The text was updated successfully, but these errors were encountered: