Skip to main content

angular - Generate random whole number

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})


export class AppComponent {
  randomNumberInt: number = -1;

  generateRandomNumberMax(max: number) {
    // Maximum number excluded 
    this.randomNumberInt =  Math.floor(Math.random() * max);
    ;
  }
}
app.component.html

<h2>Angular - Random Whole Number</h2>

<ng-template [ngIf]="randomNumberInt>=0">
    Random Number Int: {{randomNumberInt}}
</ng-template>

<br/><br/>
<button 
    (click)="generateRandomNumberMax(100)" 
    style="padding:12px;">
        Get Random Number
</button>
More Angular tutorials