mirror of
https://github.com/xuthus83/pigallery2.git
synced 2024-11-03 21:04:03 +08:00
27 lines
524 B
TypeScript
27 lines
524 B
TypeScript
|
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;
|
||
|
}
|
||
|
|
||
|
}
|