Fe-interview: [js] 第141天 写一个洗扑克牌的方法

Created on 3 Sep 2019  ·  6Comments  ·  Source: haizlin/fe-interview

第141天 写一个洗扑克牌的方法

js

Most helpful comment

let Shuffle = () =>  
    [...'A23456789JQK',10]
    .reduce( (arr, e) => arr.concat([`♥${e}`,`♠${e}`,`♣${e}`,`♦${e}`]), ['带🃏','小🃏'])
    .sort( () => Math.floor(Math.random()*2-Math.random()*2))

All 6 comments

let Shuffle = () =>  
    [...'A23456789JQK',10]
    .reduce( (arr, e) => arr.concat([`♥${e}`,`♠${e}`,`♣${e}`,`♦${e}`]), ['带🃏','小🃏'])
    .sort( () => Math.floor(Math.random()*2-Math.random()*2))

let Shuffle = () =>  
    [...'A23456789JQK',10]
    .reduce( (arr, e) => arr.concat([`♥${e}`,`♠${e}`,`♣${e}`,`♦${e}`]), ['带🃏','小🃏'])
    .sort( () => Math.floor(Math.random()*2-Math.random()*2))

秀儿 😂

@NicholasBaiYa
你就是带🃏?

求教,Math.floor(Math.random()*2-Math.random()*2) 要比 Math.random() - 0.5 更好吗?

求教,Math.floor(Math.random()*2-Math.random()*2) 要比 Math.random() - 0.5 更好吗?

[0,2] - [0,2] => [-2,2]; floor 的结果可以是 [-2,-1,0,1]
[0,1] - 0.5 => [ -0.5,0.5] ; floor 的结果只能是[ -1, 0 ]
虽然都能打乱但是 -0.5 的有更大的概率留在原地。
我还是比较喜欢自己写个for循环 来打乱顺序的毕竟sort是用来排序的,以快排为例时间复杂度也是O(nlogn) 自己写for循环的话是O(N)

求教,Math.floor(Math.random()*2-Math.random()*2) 要比 Math.random() - 0.5 更好吗?

[0,2] - [0,2] => [-2,2]; floor 的结果可以是 [-2,-1,0,1]
[0,1] - 0.5 => [ -0.5,0.5] ; floor 的结果只能是[ -1, 0 ]
虽然都能打乱但是 -0.5 的有更大的概率留在原地。
我还是比较喜欢自己写个for循环 来打乱顺序的毕竟sort是用来排序的,以快排为例时间复杂度也是O(nlogn) 自己写for循环的话是O(N)

Math.random() - 0.5 没说要 floor 吧, 就是 -0.5到0.5 不就可以了吗

Was this page helpful?
0 / 5 - 0 ratings

Related issues

haizhilin2013 picture haizhilin2013  ·  3Comments

undefinedYu picture undefinedYu  ·  3Comments

haizhilin2013 picture haizhilin2013  ·  3Comments

haizhilin2013 picture haizhilin2013  ·  3Comments

haizhilin2013 picture haizhilin2013  ·  3Comments