# Random.Range always returns same value

I have a function that fills a list of enemies randomly, but with a maximum of difficulty.
The problem is that no cares how much big is the amount of possible enemies, the list will be filled with only one of these enemies in the maximum possible amount.

This is the code:

``````	List<GameObject> RandomizeEnemies(Waves wave){
List<GameObject> list = new List<GameObject> ();
int n = wave.enemies.Length;
float waveTotalDifficulty = 0F;
float[] iDifficulty = new float[n];

for (int i = 0; i < n; i++)
{
EnemyHealth EH = wave.enemies *.GetComponent<EnemyHealth> ();*
``````
• `````` 	int health = EH.startingHealth;*
``````
• `````` 	int dmg = EH.damage;*
``````

_ float speed = wave.enemies .GetComponent ().speed;_
iDifficulty = health * dmg * speed;
* }*

* while(wave.difficulty > waveTotalDifficulty)*
* {*
* int i = Random.Range (1, n + 1);*
* list.Add (wave.enemies [i - 1]);*
* waveTotalDifficulty += iDifficulty [i - 1];*
* }*

* return list;*
* }*
What i am doing wrong?

I found a solution just taking another perspective.
In my game, i really don’t need to know all the enemies before the wave started.
So, i just simplified my function to this:

``````GameObject enemy = waves _.enemies [Random.Range (0, waves *.enemies.Length)];*_
``````

And, for anyone who has a similar problem, the final code looks like:
IEnumerator SpawnWaves(){
* yield return new WaitForSeconds (1);*
* for (int i = 0; i < waves.Length; i++)*
* {*
* float waveTotalDifficulty = 0F;*

_ while (waves .difficulty > waveTotalDifficulty)
* {
GameObject enemy = waves .enemies [Random.Range (0, waves .enemies.Length)];
waveTotalDifficulty += enemy.GetComponent ().rate;
Vector3 spawnPosition = new Vector3 (Random.Range (spawnZone.xRange.x, spawnZone.xRange.y), spawnZone.y, 1);
Instantiate (enemy, spawnPosition, Quaternion.identity);*_

_ yield return new WaitForSeconds (waves .timeBetweenEnemies);
* }*_

* yield return new WaitForSeconds (timeBetweenWaves);*
* }*
* }*