码迷,mamicode.com
首页 > 其他好文 > 详细

[TypeScript] Create random integers in a given range

时间:2017-05-01 23:42:49      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:learn   span   expec   ram   ret   exclusive   res   tar   const   

Learn how to create random integers using JavaScript / TypeScript.

 

/**
 * Returns a random int between
 * @param start inclusive
 * @param before exclusive
 */
export function randomInt(start: number, before: number) {
  return start + Math.floor(Math.random() * (before - start));
}

 

import { randomInt } from ‘./random‘;

test("Should not include ceiling", () => {
  const res = [];
  for (let index = 0; index < 100; index++) {
    res.push(randomInt(0, 5));
  }
  expect(res.some(x => x === 5)).toBeFalsy();
});

test("Should include one before ceiling", () => {
  const res = [];
  for (let index = 0; index < 100; index++) {
    res.push(randomInt(0, 5));
  }
  expect(res.some(x => x === 4)).toBeTruthy();
});

 

[TypeScript] Create random integers in a given range

标签:learn   span   expec   ram   ret   exclusive   res   tar   const   

原文地址:http://www.cnblogs.com/Answer1215/p/6793454.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!