Check out example codes for "typescript random number". It will help you in understanding the concepts better.

Code Example 1

``````/**
* Gets random int
* @param min
* @param max
* @returns random int - min & max inclusive
*/
getRandomInt(min, max) : number{
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}``````

Code Example 2

``````//Write the following code to get a random number between 0 and n
Math.floor(Math.random() * n);``````

Code Example 3

``````function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}``````

Code Example 4

``````// Between any two numbers
Math.floor(Math.random() * (max - min + 1)) + min;

// Between 0 and max
Math.floor(Math.random() * (max + 1));

// Between 1 and max
Math.floor(Math.random() * max) + 1;``````

Code Example 5

``````function getRandomNumberBetween(min,max){
return Math.floor(Math.random()*(max-min+1)+min);
}

//usage example: getRandomNumberBetween(20,400);``````

Code Example 6

``````/* If 1 argument is given, minimum will be set to 0 and maximum to this argument
* If 2 arguments were given, the fist would be the minimum and the second the maximum
* The function will return an integer in [min, max[
*/
const Math.randint => (min,max) {
[min,max] = (max===undefined)?[0,min]:(min>max)[max,min]:[min,max];
return Math.floor(Math.random*(max-min-1)+min);
}``````

Learn ReactJs, React Native from akashmittal.com