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.

import { randomInt } from '../random/random';

/**
* Returns a shuffled version of the input array
*/
export function shuffle<T>(array: T[]): T[] {
array = array.slice(); for (let i = ; i < array.length; i++) {
const randomChoiceIndex = randomInt(i, array.length);
[array[i], array[randomChoiceIndex]] = [array[randomChoiceIndex], array[i]];
} return array;
}
export function randomInt(start: number, before: number) {
return start + Math.floor(Math.random() * (before - start));
}

[Algorithom] Shuffle an array的更多相关文章

  1. [LeetCode] Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  2. 384. Shuffle an Array

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  3. Leetcode: Shuffle an Array

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  4. [Swift]LeetCode384. 打乱数组 | Shuffle an Array

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  5. 384. Shuffle an Array数组洗牌

    [抄题]: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...

  6. Java [Leetcode 384]Shuffle an Array

    题目描述: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...

  7. LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组)

    LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组) 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:h ...

  8. [LeetCode] 384. Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  9. leetcode 384. Shuffle an Array

    384. Shuffle an Array c++ random函数:https://www.jb51.net/article/124108.htm rand()不需要参数,它会返回一个从0到最大随机 ...

随机推荐

  1. asp.net webform 打造私人后台管理系统(附源码)

    系统简介 git地址:https://github.com/qingfengjun/XSSystem 不少人让我做公司的网站,要求不多,但是需要有一个后台系统,所以就有了开发的动力.也想做个系统自己学 ...

  2. 转:windbg调试堆

    转:http://www.cnblogs.com/dsky/archive/2013/05/15/3079363.html 简评: 代码中采用malloc/free进行堆申请,实际调用的仍然是Heap ...

  3. Python函数式编程——map()、reduce()

    文章来源:http://www.pythoner.com/46.html 提起map和reduce想必大家并不陌生,Google公司2003年提出了一个名为MapReduce的编程模型[1],用于处理 ...

  4. PHP文件包含小结

    协议 各种协议的使用有时是关键 file协议 file后面需是///,例如file:///d:/1.txt 也可以是file://e:/1.txt,如果是在当前盘则可以file:///1.txt 如果 ...

  5. 洛谷P3385 [模板]负环 [SPFA]

    题目传送门 题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 输入输出格式 输入格式: 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M,表示图有N个 ...

  6. 关于std::ios::sync_with_stdio(false)

    std::ios::sync_with_stdio(false); 很多C++的初学者可能会被这个问题困扰,经常出现程序无故超时,最终发现问题处在cin和cout上,(甚至有些老oier也会被这个问题 ...

  7. 面向对象编程课程(OOP)第二单元总结

    一.设计策略 第一次作业(傻瓜式电梯): 由于是第一次写多线程作业,许多的知识还处在理论阶段,所以第一次作业写得非常的朴实无华.整个程序总共有四个类,Main类负责通过电梯类实例化一个电梯,然后通过w ...

  8. 【Spring学习】在Spring+Maven环境中使用Junit Test

    在Spring+Maven环境中使用Junit Test 前言 以前我是很讨厌写测试代码的,总觉得测试用例是测试人员写的,现在想想自己真是Too yuong too simple,接触开发多了之后发现 ...

  9. 【BZOJ 3924】【ZJOI 2015】幻想乡战略游戏

    http://www.lydsy.com/JudgeOnline/problem.php?id=3924 gty的测试题,不会动态点分治而且看不出来链剖做法而且暴力打残所以这道题喜闻乐见的爆零了qwq ...

  10. poj 3225 Help with Intervals(线段树,区间更新)

    Help with Intervals Time Limit: 6000MS   Memory Limit: 131072K Total Submissions: 12474   Accepted:  ...