# how to create a random with probability ?

Hi,

I’m trying to create a Random with probability.

I have a list of number from 0 to 10 and I generate 100 numbers from this list. My problem is I would like to generate exactly 6 times the number 10.

I tried Random.value but it’s not accurate.

Is someone can help me?

Bye

``````public List<int> number = new List<int> {1,2,3,4,5,6,7,8,9,10};

public float timer;
public int index;
public int countGeneration;
public int count_5;

// Use this for initialization
void Start()
{
timer = .01f;
index = 0;
countGeneration = 0;
count_5 = 0;
}

// Update is called once per frame
void Update ()
{
timer -= Time.deltaTime;

if (timer <= 0 && countGeneration < 100)
{
timer = .01f;
float rand = Random.value;
if (rand <= .94f)
index = Random.Range(0, 9);
else
index = 10;

if (index == 10)
count_5++;

countGeneration++;
}
}
``````

The only way to get some number to occur exactly N times is to do it explicitly, without using random. Since I assume you still want a random order, you need to generate first, then shuffle. The steps are:

1. Create a `List< int >`.
2. Add the number 10 six times.
3. Add 94 random numbers in the range 0-9.
4. Shuffle the list.

int random10Indexs = new int[6];
int countGeneration;
int index;

``````void Start()
{
bool isFirst = true;

for (int i = 0; i < 6; i++)
{
if (isFirst)
{
int index = Random.Range(0, 100);
random10Indexs *= index;*
``````

isFirst = false;
}
else
{
aaa:
int index = Random.Range(0, 100);
for (int j = 0; j < i; j++)
{
if (index == random10Indexs[j])
goto aaa;
}
random10Indexs = index;
}
}
}
void Update()
{
bool is10 = false;
if (countGeneration < 100)
{
for (int i = 0; i < random10Indexs.Length; i++)
{
if (countGeneration == random10Indexs*)*
is10 = true;
}
if (is10)
index = 9;
else
index = Random.Range(0, 8);
countGeneration++;
}
}
Indexes that produced from the Update are from 0 to 9, to get the actual numbers 1-10 either you use them to refer to your List with the numbers or change the values one up to get directly what you want. Thats the easiest way to ensure you have 6 * 10s and the rest have the same probability to be produced. Hope that helps. Another way to do this would be to create a pool with all the numbers and start picking one by one and removing them from the pool. There are several ways to go around that problem and each have other positives and negatives. That one is closer to random but not the best for cpu and memory usage. Hope that helps , for clarifications ask away.