1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/model/seededRandom.service.ts

27 lines
524 B
TypeScript
Raw Normal View History

2019-01-28 06:27:41 +08:00
import {Injectable} from '@angular/core';
@Injectable()
export class SeededRandomService {
private static readonly baseSeed = Math.random() * 2147483647;
private seed: number;
constructor() {
this.setSeed(0);
if (this.seed <= 0) {
this.seed += 2147483646;
}
}
setSeed(seed: number) {
this.seed = (SeededRandomService.baseSeed + seed) % 2147483647; // shifting with 16 to the left
}
get() {
this.seed = (this.seed * 16807 % 2147483647);
return this.seed / 2147483647;
}
}