generate a random boolean in typescript

You can generate a random boolean in TypeScript using the Math.random() function, which returns a random floating-point number between 0 and 1. You can then compare the value to 0.5 to get a 50/50 chance of returning true or false. Here's an example function:

index.ts
function randomBoolean(): boolean {
  return Math.random() >= 0.5;
}
69 chars
4 lines

This function returns true if the result of Math.random() is greater than or equal to 0.5, and false otherwise.

gistlibby LogSnag