ゲーム向けの素材の作り方やノウハウを補完していこうってwikiです。フリー素材もあるよ。

0〜9の整数値をランダムで生成する。
ただし、2回目以降は必ず前回と異なる数値を出力する。

GameMaker


//Execute Codeで以下のコードを実行
//ただし変数valueは別の場所で0〜9以外の値に初期化されているものとする

do {
    temp = floor(random(10));
} until(temp != value)

value = temp;

Tonyu


//初期化
value = -1;

//ループ
while(1) {
    //(別の処理)

    //値の取得
    temp = rnd(10);
    while(temp == value) temp = rnd(10);
    value = temp;

    //(別の処理)
}

Java


/**
 * 乱数生成用クラス
 * 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/

0
Posted by vulljr 2013年11月20日(水) 18:54:38 返信

uFMd4u <a href="http://yrkpwbwfwnfa.com/">yrkpwbwfwnfa</a>, [url=http://vvvhfwnitdtm.com/]vvvhfwnitdtm[/url], [link=http://spybjlzqkfsz.com/]spybjlzqkfsz[/link], http://xuwusgfcehns.com/

0
Posted by pkzkecg 2013年11月14日(木) 14:20:33 返信

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

どなたでも編集できます