Skip to content
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

Open
ramcandrews opened this issue Jan 9, 2021 · 5 comments
Open

The Array sort is not very random. #11

ramcandrews opened this issue Jan 9, 2021 · 5 comments

Comments

@ramcandrews
Copy link

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.

@ramcandrews
Copy link
Author

This is a better alternative to array.sort()

Fisher-Yates Algorithm

author : nitinpatel_20236

  for(let i = cardArray.length — 1; i > 0; i--){
    const j = Math.floor(Math.random() * i)
    const temp = cardArray[i]
    cardArray[i] = cardArray[j]
    cardArray[j] = temp
  }

@kubowania
Copy link
Owner

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

@ramcandrews
Copy link
Author

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.

@kubowania
Copy link
Owner

kubowania commented Jan 9, 2021

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:
let myA = [32, 2, 10, 100, 44]
myA.sort(() => 0.5 - Math.random())

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.

@ramcandrews
Copy link
Author

ramcandrews commented Jan 9, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants