# How to put Random.range in float array?

Hi I’m not sure am I correctly ask, sorry if i make mistake.

I have a script which have an array of float values. This script is holding data about when i kill an enemy. It adds value of pointsToEarn to my general score. I want to make it randomize. I dont want to give “pointsToEarn” a static value, I want to give it a random value, such as random.range(1,5). But I dont understand how should i put Random.Range inside it.

Can you help me? Thank you for your patience.

``````public float[] pointsToEarn = new float;

(...)

Anotherscript.anotherFunction(0, pointsToEarn);
``````

From what I can tell, you’re trying to add a score of 1 to 5 randomly. This is the best way to do it:

``````public float maxPointsToEarn = 5;

// add to score (yours may be different)
{
score += points;
}

// somewhere else
``````

I hope this ain’t too much. But I wanted to show three ways of using random numbers with arrays.

Start calls 3 functions. Each of them shows a different way to use Random.Range.

• The first example just stores random values into an array.
• The second example pick random values from an array.
• The third example got random ranges in the array. To clarify, you could access `knownRanges.Value` 10 times, get 10 random values each time, but within the range of [100, 150].

RandomRangeArrayExamples.cs

``````using UnityEngine;
using StringBuilder = System.Text.StringBuilder;

public class RandomRangeArrayExamples : MonoBehaviour
{
StringBuilder logger = new StringBuilder();

void Start()
{
DemoRandomNumbersInArray();
DemoKnownNumbersInArray();
DemoKnownRangesInArray();
print(logger);
}

/*
DemoRandomNumbersInArray
number > 7.994198
number > 0.01712418
number > -7.934887
number > -3.391624
*/
void DemoRandomNumbersInArray()
{
logger.AppendLine("DemoRandomNumbersInArray");

// 4 randomly generated numbers between -10 and 10
float[] randomNumbers = {
Random.Range(-10f, 10f),
Random.Range(-10f, 10f),
Random.Range(-10f, 10f),
Random.Range(-10f, 10f)
};

// each number in the array is random
foreach (float randomNumber in randomNumbers)
logger.AppendLine("  number > " + randomNumber);
logger.AppendLine();
}

/*
DemoKnownNumbersInArray
index 0 > 100
index 2 > 300
index 2 > 300
index 0 > 100
*/
void DemoKnownNumbersInArray()
{
logger.AppendLine("DemoKnownNumbersInArray");

// 4 chosen numbers, accessed randomly
float[] knownNumbers = {
100,
200,
300,
500
};

for (int i = 0; i < 4; ++i)
{
int randomIndex = Random.Range(0, knownNumbers.Length);
float randomNumber = knownNumbers[randomIndex];
logger.AppendLine("  index " + randomIndex + " > " + randomNumber);
}
logger.AppendLine();
}

/*
DemoKnownRangesInArray
range[100, 150] > 121.7349
range[200, 250] > 233.4365
range[300, 350] > 335.7147
range[500, 550] > 529.447
*/
void DemoKnownRangesInArray()
{
logger.AppendLine("DemoKnownRangesInArray");

// 4 chosen ranges of random numbers
RandomRange[] knownRanges = {
new RandomRange(100, 150),
new RandomRange(200, 250),
new RandomRange(300, 350),
new RandomRange(500, 550)
};

// each range in the array generate a random number
foreach (RandomRange knownRange in knownRanges)
logger.AppendLine("  range " + knownRange + " > " + knownRange.Value);
logger.AppendLine();
}
}

[System.Serializable]
public class RandomRange
{
public float min;
public float max;
public float Value { get { return Random.Range(min, max); } }

public RandomRange(int min, int max)
{
this.min = min;
this.max = max;
}

public override string ToString()
{
return "[" + min + ", " + max + "]";
}
}
``````

something like this:
`pointsToEarn = UnityEngine.Random.Range(-10f, 10f)` ?