Unity:乱数課題解答例

Unity

課題内容

1 ~ 3 までの範囲の整数型乱数を変数nに生成し、コンソール出力させる。
-5.0 ~ 5.0 までの範囲の実数型乱数を変数mに生成し、コンソール出力させる。

C#スクリプト例

Ransu.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Ransu : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //1 ~ 3 までの範囲の整数型乱数を変数nに生成し、コンソール出力させる。
        int n = Random.Range(1, 4);
        Debug.Log("n = " + n);
        // -5.0 ~ 5.0 までの範囲の実数型乱数を変数mに生成し、コンソール出力させる
        float m = Random.Range(-5.0f, 5.0f);
        Debug.Log("m = " + m);
    }
 
    // Update is called once per frame
    void Update()
    {
         
    }
}

コメント