Well, all you need is some simple math like this:
// C#
int num = Random.Range(7,25); // 7 .. 24 since 25 is exclusive.
if (num > 15)
num = 9-num; // (9-16) .. (9-24) == -7 .. -15
// num == 7 .. 15 or -15 .. -7
edit
I have the feeling i should add some explanation. First of all you should figure out how many possible numbers you want to get in total. 7 to 15 and -15 to -7 are 9 values each. So there are 18 possible values. The Random.Range goes from 7 to 24 which has a range of 18 values. The lower 9 values is already our positive range (7 to 15) if we get a value larger then 15 we map the upper 9 values (16 to 24) down to (-7 to -15)
A more general approach could be something like this:
// C#
public struct Range
{
public int min;
public int max;
public int range {get {return max-min + 1;}}
public Range(int aMin, int aMax)
{
min = aMin; max = aMax;
}
}
public static int RandomValueFromRanges(params Range[] ranges)
{
if (ranges.Length == 0)
return 0;
int count = 0;
foreach(Range r in ranges)
count += r.range;
int sel = Random.Range(0,count);
foreach(Range r in ranges)
{
if (sel < r.range)
{
return r.min + sel;
}
sel -= r.range;
}
throw new Exception("This should never happen");
}
In this example the “Ranges” are max-inclusive. So in your case you would do:
int val = RandomValueFromRanges(new Range(-15,-7), new Range(7,15));
If you want the ranges to be max-exclusive you have to remove the “+1” in the range property. In this case you would have to do this to get the same result:
int val = RandomValueFromRanges(new Range(-15,-6), new Range(7,16));
Depending on your usecase when you want a relative offset with a min and max distance you would use a vector, a random direction and then just scale it between your min and max value.
public static Vector2 RandomXYOffset(float aMin, float aMax)
{
float angle = Random.value * Mathf.PI * 2;
Vector2 dir = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
return dir * Random.Range(aMin, aMax);
}
The offset is of course not grid aligned but since you didn’t said your actual usecase we can only guess.