LoginSignup
3
0

More than 1 year has passed since last update.

【完走目指す】初心者が25日で強くなる Advent Calendar 2022
12日目の記事です。遅れました。明日も遅れます。

Random

ランダムです。
範囲を指定してあげれば、その中から無作為に一つ選んでくれます。便利です。
Unityにもランダムが使えます。ところで、Unityでrandomを使ったら、偏りはないのでしょうか。調べてみました。

流れ

1.10000回ランダムに1~7までの数字を出す
2.A~Gに、出た数をそれぞれ足していく。
3.1~7それぞれ何回出たか、A~Gを見て調べる。
こんな感じです。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Benkyou : MonoBehaviour
{
    int K;
    int U;
    public int A;
    public int B;
    public int C;
    public int D;
    public int E;
    public int F;
    public int G;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        for (; U <= 9999; U++) {
            K = Random.Range(1, 8);
            switch (K)
            {
                case 1:
                    A = A + 1;
                    break;
                case 2:
                    B = B + 1;
                    break;
                case 3:
                    C = C + 1;
                    break;
                case 4:
                    D = D + 1;
                    break;
                case 5:
                    E = E + 1;
                    break;
                case 6:
                    F = F + 1;
                    break;
                case 7:
                    G = G + 1;
                    break;
            }
        }
        
    }
    
}

こんな感じです。

結果

以下のようになりました。
スクリーンショット_20221211_172525.png

大体1400から1500回の間に収まっています。大幅な偏りはありませんでした。
これは、
ランダム1.png
こんな感じです。

偏りを作る

偏りがないのはわかりました。
では次に、意図的に偏りを作ります。
ゲームのガチャとか、強いキャラは出にくくなります。
しかし、今のように偏りがあるとすべて同じ確率で出てしまいます。
偏りがあることも時には大切なのです。後で使うので、作ってみます。
以下は、偏りのある図です。
確率2 random.png
スタートで右か左のどちらかに進めます。
ランダムに選んで進んだ先、また左右を選びます。
これの繰り返しです。
両端はすべて同じ方向に進み続ける必要がありますが、真ん中のほうは最終的に真ん中に行けばいいのでいくつか方法があります。

作ってみた

中身はこんな感じです。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Benkyou2 : MonoBehaviour
{
    public int U;
    public int CaA;
    public int CaB;
    public int CaC;
    public int CaD;
    public int CaE;
    public int CaF;
    public int CaG;
    public int A;
    public int B;
    public int C;
    public int D;
    public int E;
    public int F;
    public int G;

    // Start is called before the first frame update
    void Start()

    {

    }

    // Update is called once per frame
    void Update()
    {
        {for (; U < 10000; U++)
            {
                CaA = Random.Range(1, 3);
                if (CaA == 1)
                {
                    CaB = Random.Range(3, 5);

                }
                if (CaA == 2)
                {
                    CaB = Random.Range(4, 6);
                }
                if(CaB==3)
                {
                    CaC = Random.Range(6, 8);
                }
                if (CaB == 4)
                {
                    CaC = Random.Range(7, 9);
                }
               
                    if (CaB == 5)
                    {
                        CaC = Random.Range(8, 10);
                    }
                   


                
                if (CaC == 6)
                {
                    CaD = Random.Range(10, 12);

                }
                if (CaC == 7)
                {
                    CaD = Random.Range(11, 13);

                }
                if (CaC == 8)
                {
                    CaD = Random.Range(12, 14);

                }
                if (CaC == 9)
                {
                    CaD = Random.Range(13, 15);

                }
                if (CaD == 10)
                {
                    CaE = Random.Range(15, 16);

                }
                if (CaD == 11)
                {
                    CaE = Random.Range(16, 18);

                }
                if (CaD == 12)
                {
                    CaE = Random.Range(17, 19);

                }
                if (CaD == 13)
                {
                    CaE = Random.Range(18, 20);

                }
                if (CaD == 14)
                {
                    CaE = Random.Range(19, 21);

                }
                if (CaE == 15)
                {
                    CaF = Random.Range(21, 23);

                }
                if (CaE == 16)
                {
                    CaF = Random.Range(22, 24);

                }
                if (CaE == 17)
                {
                    CaF = Random.Range(23, 25);


                }
                if (CaE == 18)
                {

                    CaF = Random.Range(24, 26);


                }
                if (CaE == 19)
                {
                    CaF = Random.Range(25, 27);


                }
                if (CaE == 20)
                {

                    CaF = Random.Range(26, 28);


                }
                if (CaF == 21)
                {
                    A = A + 1;
                }
                if (CaF == 22)
                {
                    B=B+ 1;
                }
                if (CaF == 23)
                {
                    C = C + 1;
                }
                if (CaF == 24)
                {
                    D = D + 1;
                }
                if (CaF == 25)
                {
                    E = E + 1;
                }

                if (CaF == 26)
                {
                    F = F + 1;
                }
                if(CaF==27)
                {
                    G = G + 1;
                }



            }
        }
    }
}

switchだとうまく動作しないみたいです。ifをたくさん使いました。
結果は以下の通りです。
スクリーンショット_20221212_102601.png
真ん中のDが3000以上あるのに対し、AとGは数百しかありません。

ランダムなくじ

では、ランダムなくじを作ります。
今回は、偏りのあるほうを使います。
以下が画面です。

中身

以下のように記述します。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Kuji1 : MonoBehaviour
{
  
    public int CaA;
    public int CaB;
    public int CaC;
    public int CaD;
    public int CaE;
    public int CaF;
    public int CaG;
    public int A;
    public int B;
    public int C;
    public int D;
    public int E;
    public int F;
    public int G;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void OnClick()
    {
        CaA = Random.Range(1, 3);
        if (CaA == 1)
        {
            CaB = Random.Range(3, 5);

        }
        if (CaA == 2)
        {
            CaB = Random.Range(4, 6);
        }
        if (CaB == 3)
        {
            CaC = Random.Range(6, 8);
        }
        if (CaB == 4)
        {
            CaC = Random.Range(7, 9);
        }

        if (CaB == 5)
        {
            CaC = Random.Range(8, 10);
        }




        if (CaC == 6)
        {
            CaD = Random.Range(10, 12);

        }
        if (CaC == 7)
        {
            CaD = Random.Range(11, 13);

        }
        if (CaC == 8)
        {
            CaD = Random.Range(12, 14);

        }
        if (CaC == 9)
        {
            CaD = Random.Range(13, 15);

        }
        if (CaD == 10)
        {
            CaE = Random.Range(15, 16);

        }
        if (CaD == 11)
        {
            CaE = Random.Range(16, 18);

        }
        if (CaD == 12)
        {
            CaE = Random.Range(17, 19);

        }
        if (CaD == 13)
        {
            CaE = Random.Range(18, 20);

        }
        if (CaD == 14)
        {
            CaE = Random.Range(19, 21);

        }
        if (CaE == 15)
        {
            CaF = Random.Range(21, 23);

        }
        if (CaE == 16)
        {
            CaF = Random.Range(22, 24);

        }
        if (CaE == 17)
        {
            CaF = Random.Range(23, 25);


        }
        if (CaE == 18)
        {

            CaF = Random.Range(24, 26);


        }
        if (CaE == 19)
        {
            CaF = Random.Range(25, 27);


        }
        if (CaE == 20)
        {

            CaF = Random.Range(26, 28);


        }
        if (CaF == 21)
        {
            A = A + 1;
        }
        if (CaF == 22)
        {
            B = B + 1;
        }
        if (CaF == 23)
        {
            C = C + 1;
        }
        if (CaF == 24)
        {
            D = D + 1;
        }
        if (CaF == 25)
        {
            E = E + 1;
        }

        if (CaF == 26)
        {
            F = F + 1;
        }
        if (CaF == 27)
        {
            G = G + 1;
        }



    

    }
}

ボタンが押されると、1回くじが引かれます。
AかGが出ると1等
BかFが出ると2等
CかEが出ると3等
Dははずれです。
あと、現在の試行回数と出た等が見れるようにします。
試行回数:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class NowD : MonoBehaviour
{ public Kuji1 CT;
    public TextMeshProUGUI NowK;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        NowK.text =""+  CT.ChkL;
    }
}

出る等数:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using TMPro;
public class ChkT : MonoBehaviour
{ public Kuji1 kuji;
    public TextMeshProUGUI Tousuu;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Tousuu.text = ""+(kuji.A + kuji.G);
        
    }
}

これで動くでしょう。
Unityの、TextMeshProの設定は忘れがちなので注意。
当初「1等」とか「試行回数」をTextにそのまま出そうとしていましたが、あまりに時間がかかったので画像に変更しました
以下が画像です。
12日くじ.png
そして結果がこちら(100回しかやってませんが)

image.png

結果

動いた
くじっぽくなった

まとめ

くじを作るならこれでいいでしょう。
明日も遅れます。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0