Shuffling is a common process used with randomizing the order for a deck of cards. The key property for a perfect shuffle is that each item should have an equal probability to end up in any given index.

In this lesson we discuss the concept behind the simple modern fisher yates shuffle and implement it in JavaScript / TypeScript.

function randomInt(start: number, before: number) {
return start + Math.floor(Math.random() * (before - start));
} function shuffle<T>(array: T[]): T[] {
array = array.slice(); for (let i = 0; i < array.length; i++) {
const randomChoiceIndex = randomInt(i, array.length);
[array[i], array[randomChoiceIndex]] = [array[randomChoiceIndex], array[i]]; // swap element in array
} return array;
}

最新文章

  1. C#利用反射+特性实现简单的实体映射数据库操作类
  2. Count(*)或者Count(1)或者Count([列]) 区别
  3. Android双击Back退出应用
  4. 关于微信扫描二维码下载apk文件的细节设计
  5. 【转】Hive学习路线图
  6. cocos2d-x 添加背景音乐和音效-SimpleAudioEngine
  7. 【Java基础】关于String的总结
  8. 【转】模块编译Android源码方法
  9. AngularJs练习Demo11引入Jquery
  10. JedisPool操作
  11. VS2017 Cordova Ionic2 移动开发-环境搭建
  12. 第一章:大数据 の Linux 基础 [更新中]
  13. hive中使用case、if:一个region统计业务(hive条件函数case、if、COALESCE语法介绍:CONDITIONAL FUNCTIONS IN HIVE)
  14. ToastCustomUtil【简单的Toast封装类】【自定义Toast的显示风格】
  15. Java运行原理、三大体系、jdk构成
  16. JAVA字符串的常见处理和操作
  17. ACM-ICPC 2018 南京赛区网络预赛 E AC Challenge(状压dp)
  18. centos7 与 archlinux用户 安装 python3模块 pytaglib
  19. PHP 将数组的值赋值给一组变量
  20. 初识waindows窗体程序错题整理

热门文章

  1. js插件---图片懒加载echo.js结合 Amaze UI ScrollSpy 使用
  2. Android自定义组件系列【16】——最帅气的自动滚动广告条
  3. win7防火墙里开启端口的图文教程
  4. python 协程学习
  5. java中锁的理解
  6. Maven学习总结(20)——Maven pom.xml配置再体会
  7. BZOJ——T 1113: [Poi2008]海报PLA
  8. 压缩感知——SP(subspace pursuit)重构算法前言翻译
  9. android ActionBar的使用
  10. 实战c++中的string系列--string的替换、查找(一些与路径相关的操作)