using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandmR : MonoBehaviour
{
private int[] testArray;
private int total;
private int count;
private int[] sequence;
private int[] output;
// Start is called before the first frame update
void Start()
{
total = 5;
count = 1;
testArray = GetRandomSequence(total, count);
for (int i = 0; i < count; i++)
{
print(testArray[i]);
}
print("-----------------------");
count = 3;
testArray = GetRandomSequence(total, count);
for (int i = 0; i < count; i++)
{
print(testArray[i]);
}
print("-----------------------");
count = 5;
testArray = GetRandomSequence(total, count);
for (int i = 0; i < count; i++)
{
print(testArray[i]);
}
}
// Update is called once per frame
void Update()
{
}
public int[] GetRandomSequence(int total, int count)
{
sequence = new int[total];
output = new int[count];
for (int i = 0; i < total; i++)
{
sequence[i] = i;
}
int end = total - 1;
for (int i = 0; i < count; i++)
{
int num = Random.Range(0, end + 1);
output[i] = sequence[num];
sequence[num]= sequence[end];
end--;
}
return output;
}
}
2
-----------------------
0
1
2
-----------------------
1
2
3
0
4
|