- Home /
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[2];
(...)
Anotherscript.anotherFunction(0, pointsToEarn[0]);
Hello!
I need to clarify: do you want to make random array's length in new float[2]? Or you want to extract value by random index in Anotherscript.anotherFunction(0, pointsToEarn[0]);?
Hello! and thanks for help.
I wanto to "add" or "make" random value to pointsToEarn[0]. Something like;
Anotherscript.anotherFunction(0, pointsToEarn[0].Random.Range(1,5));
ah I understand, you told me declare this before put in value... Ok got it. And it works, thanks.
Answer by Denvery · Oct 11, 2015 at 08:47 PM
something like this: pointsToEarn[0] = UnityEngine.Random.Range(-10f, 10f)
?
if you can convert it to answer, then i can vote it for best answer.
Answer by willparsons · Oct 11, 2015 at 09:03 PM
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)
void AddScore(float points)
{
score += points;
}
// somewhere else
AddScore(Random.Range(1, maxPointsToEarn));
thank you for your help. Your method is also useful, but
pointsToEarn[0] = UnityEngine.Random.Range(-10f, 10f)
is what i need.
Answer by Statement · Oct 11, 2015 at 09:39 PM
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[0].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
};
// each access to the array is random
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 + "]";
}
}
Thank you very much. Yes, its little bit too much for me, trying to understand:) It'a great example, father of all examples:)
It's good feedback :) Often the simplest answer is the best. I had problems understanding what you were asking so I pulled out the boomstick.