シンプルな質問ですが、答えが見つからない、数学でかなり悪いです。問題は、私はこの数式を乱数を生成することです。Math.Randomが正しく動作しないタイプスクリプト
numRandomed: any;
constructor(public navCtrl: NavController, public navParams: NavParams) {
}
public randomize(min, max) {
console.log("minimo: " + min);
console.log("maximo: " + max);
this.numRandomed = Math.floor(Math.random()*(max-min+1)+min);
console.log(this.numRandomed);
}
とビュー
<ion-list>
<ion-item>
<ion-label floating>Min Number</ion-label>
<ion-input type="number" [(ngModel)]='minNumber'></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Max Number</ion-label>
<ion-input type="number" [(ngModel)]='maxNumber'></ion-input>
</ion-item>
</ion-list>
<button ion-button (click)='randomize(minNumber,maxNumber)'>Randomize!</button>
<p>{{ numRandomed }}</p>
しかし、乱数は、常に最小数よりも小さい範囲内で、私はどれなぜ...
を知らないされていません手がかり?ありがとうございました!!
'randomize'クラス/オブジェクトを使用せず、すぐにそのフィールドの値を返すときに、' randomize'プロトタイプに 'numRandomed'フィールドを追加するのはなぜですか? – LutzL
私は{{numRandomed}}のビューでその変数を使用しています 私はビューのボタンで関数を呼び出しています。 – Mystearica
これは閉鎖のいくつかの種類ですか、閉鎖に正しいフォームを使用していることを確信していますか? 'let me = this;'を定義してから 'me.numRandomed'に代入する必要はありませんか? – LutzL