[TypeScript] Create random integers in a given range

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();
});
时间: 2024-10-09 17:10:44

[TypeScript] Create random integers in a given range的相关文章

Java – Generate random integers in a range

In this article, we will show you three ways to generate random integers in a range. java.util.Random.nextIntMath.randomjava.util.Random.ints (Java 8)1. java.util.RandomThis Random().nextInt(int bound) generates a random integer from 0 (inclusive) to

[TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers

Using the optional “+” sign together with mapped type modifiers, we can create more explicit and readable type declarations. We can also use the “-” (minus) sign to remove optional declarations from properties. For example, we have an interface: inte

8.模块介绍 time &amp;datetime模块 random os sys shutil json &amp; picle shelve xml处理 yaml处理 configparser hashlib subprocess logging模块 re正则表达式

本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configparser hashlib subprocess logging模块 re正则表达式 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.p

java.util.Random 类的 nextInt(int num )

随机产生3个67~295的整数并找出数值居中的数 并输出中间的数例如:100,225和200,输出200 要随机产生某个范围内的整数,用 java.util.Random 类的 nextInt(int num) 最简洁. nextInt( int num) 能接受一个整数作为它所产生的随机整数的上限,下限为零,比如:nextInt(4)将产生0,1,2,3这4个数字中的任何一个数字,注意这里不是0-4,而是0-3..但下限总是零,不能更改,所以若要达到非零下限的效果,必须把上限减去下限的结果传给

OpenCV Tutorials &mdash;&mdash; Random generator and text with OpenCV

creating a Random Number Generator object (RNG): RNG rng( 0xFFFFFFFF ); 创建并初始化随机数生成子 create a matrix initialized to zeros (which means that it will appear as black), specifying its height, width and its type: /// Initialize a matrix filled with zeros

random 模块

random 模块包含许多随机数生成器,用于生成随机数 使用 random 模块获得随机数字 1. random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 import random print random.random() # 0.403805661236 2. random.uniform(a,b)用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限.如果a > b,则生成的随机数n: a >= n >= b.如果 a

一道题目- Find the smallest range that includes at least one number from each of the k lists

You have k lists of sorted integers. Find the smallest range that includes at least one number from each of the k lists. For example, List 1: [4, 10, 15, 24, 26] List 2: [0, 9, 12, 20] List 3: [5, 18, 22, 30] The smallest range here would be [20, 24]

How do I create a List in Scala?

Scala List class FAQ: How do I create a List in Scala? You can create a Scala List in several different ways, including these approaches: Lisp style Java style Using the List class rangemethod Using the List class fillmethod Using the List classtabul

Getting started with TypeScript and Sublime Text -- 摘自https://cmatskas.com/getting-started-with-typescript-and-sublime-text/

Getting started with TypeScript and Sublime Text 04 March 2015  18 Comments  Posted in JavaScript, Open Source, TypeScript, Sublime Text UPDATED: This post has been rewritten around the official TypeScript plugin Typescript is awesome, period. TypeSc