最終更新:ID:oZ19cK16eA 2011年03月02日(水) 17:59:15履歴
0〜9の整数値をランダムで生成する。
ただし、2回目以降は必ず前回と異なる数値を出力する。
ただし、2回目以降は必ず前回と異なる数値を出力する。
//Execute Codeで以下のコードを実行 //ただし変数valueは別の場所で0〜9以外の値に初期化されているものとする do { temp = floor(random(10)); } until(temp != value) value = temp;
//初期化 value = -1; //ループ while(1) { //(別の処理) //値の取得 temp = rnd(10); while(temp == value) temp = rnd(10); value = temp; //(別の処理) }
/** * 乱数生成用クラス * minとmaxの指定は再利用性を考えての処置 */ public class RandomIntCreator { private int min; private int max; private int before; public RandomIntCreator(int min, int max) { if(max <= min) throw new IllegalArgumentException(); this.min = min; this.max = max; this.before = min - 1; } public int get() { while(true) { int temp = (int) (Math.random() * (max - min + 1) + min); if(temp != before) return temp; } } } /** * 利用例 */ public class User { private RandomIntCreator rnd = new RandomIntCreator(0, 9); public void use() { //(別の処理) //値の取得 int value = rnd.get(); //(別の処理) } }
このページへのコメント
TiMgjc <a href="http://qvzmmeqgwaqq.com/">qvzmmeqgwaqq</a>, [url=http://tjlznimqygcy.com/]tjlznimqygcy[/url], [link=http://lrpeyfnwsuqr.com/]lrpeyfnwsuqr[/link], http://oooszeovzjmz.com/
uFMd4u <a href="http://yrkpwbwfwnfa.com/">yrkpwbwfwnfa</a>, [url=http://vvvhfwnitdtm.com/]vvvhfwnitdtm[/url], [link=http://spybjlzqkfsz.com/]spybjlzqkfsz[/link], http://xuwusgfcehns.com/