- Home /

# Random.Range Vector3

I'm wondering if there's a simple way to generate a random Vector3 given maximum and minimum Vector3's.

So far what I've been doing works, but it seems clunky. Is there a better way?

```
var minPosition : Vector3;
var maxPosition : Vector3;
var randomPosition = Vector3(Random.Range(minPosition.x, maxPosition.x), Random.Range(minPosition.y, maxPosition.y), Random.Range(minPosition.z, maxPosition.z) );
```

Ideally I'd like to do something like:

```
var minPosition : Vector3;
var maxPosition : Vector3;
var randomPosition : Vector3 = Random.Range(minPosition, maxPosition);
```

Is there a better way to find a random value between two Vector3's?

```
Random.onUnitSphere * Random.Range(minDistance, maxDistance);
```

That Would give you a random position between a min and max distance.

@MikeNewall - your code was my first quick thought. But if you look at his code, it generates a set of random values within an axes aligned rectangular box at an arbitrary position. Your code generates a shell of some thickness with Vector3.zero as the origin.

If you wanted a spherical collection of points around some fixed position, you could do:

var randomPosition = center + Random.insideUnitSphere * maxDist;

@roertbu I should read things properly :p Also I forgot about

```
Random.insideUnitSphere * radius;
```

which would give the same result as his code but with spherical bounds instead of rectangular.

**Answer** by wibble82
·
Apr 25, 2014 at 05:18 PM

Your code effectively gives you a random position within a 'box', who's "min" corner is at minPosition and "max" corner is at maxPosition. There's no handy unity function to return you a random vector within a box unfortunately, however you could of course write your own little utility function to do it. Maybe create a class somewhere called 'RandomUtils', with a static function in called 'RandomVectorInBox'.

The Random.onUnitSphere, or Random.insideUnitSphere functions give you a random vector either on the surface of, or inside, a sphere of radius 1. Even if you then multiple the result by a random number you won't end up with an even spread and they wouldn't match the bounds of your box either. Hence I wouldn't recommend doing something with them.

So the simple answer is no - there isn't a nicer way of doing it! But you could write your own little utility function to do it to make your code a little cleaner.

@wibble82, I like your explanation. I never really thought about the min and max forming a box. Too bad there's no Unity function for this. I'll continue doing what I've been doing for now. I'll probably end up writing a utility function like you suggested since this seems to be something I do fairly often.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.