Skip to content

Instantly share code, notes, and snippets.

@Ashwinning
Last active November 6, 2026 08:49
Show Gist options
  • Select an option

  • Save Ashwinning/269f79bef5b1d6ee1f83 to your computer and use it in GitHub Desktop.

Select an option

Save Ashwinning/269f79bef5b1d6ee1f83 to your computer and use it in GitHub Desktop.
Find a random point between two Vector3 points. #gistblog #unity3d #c#
/// <summary>
/// Samples a point on the line segment from min to max, including both endpoints.
/// </summary>
/// <param name="min">The start of the segment.</param>
/// <param name="max">The end of the segment.</param>
public static Vector3 GetRandomVector3Between(Vector3 min, Vector3 max)
{
return Vector3.Lerp(min, max, UnityEngine.Random.value);
}
/// <summary>
/// Samples a point on the segment remaining after trimming both ends.
/// The trimmed endpoints are included.
/// </summary>
/// <param name="min">The start of the segment.</param>
/// <param name="max">The end of the segment.</param>
/// <param name="minPadding">Distance to trim from min, in Unity units.</param>
/// <param name="maxPadding">Distance to trim from max, in Unity units.</param>
/// <exception cref="System.ArgumentOutOfRangeException">
/// A padding value is negative or non-finite, or the total exceeds the segment length.
/// </exception>
public static Vector3 GetRandomVector3Between(Vector3 min, Vector3 max, float minPadding, float maxPadding)
{
if (float.IsNaN(minPadding) || float.IsInfinity(minPadding) || minPadding < 0f)
{
throw new System.ArgumentOutOfRangeException(nameof(minPadding), "Padding must be finite and non-negative.");
}
if (float.IsNaN(maxPadding) || float.IsInfinity(maxPadding) || maxPadding < 0f)
{
throw new System.ArgumentOutOfRangeException(nameof(maxPadding), "Padding must be finite and non-negative.");
}
float distance = Vector3.Distance(min, max);
float totalPadding = minPadding + maxPadding;
if (totalPadding > distance)
{
throw new System.ArgumentOutOfRangeException(nameof(maxPadding), "Combined padding cannot exceed the segment length.");
}
if (distance == 0f)
{
return min;
}
Vector3 start = Vector3.Lerp(min, max, minPadding / distance);
if (totalPadding == distance)
{
return start;
}
Vector3 end = Vector3.Lerp(max, min, maxPadding / distance);
return GetRandomVector3Between(start, end);
}
@Kyonru

Kyonru commented Jun 18, 2020

Copy link
Copy Markdown

I would suggest changing Random.Range (0, 1) to Random.Range (0f, 1f).

@brucemax

Copy link
Copy Markdown

I would suggest changing Random.Range (0, 1) to Random.Range (0f, 1f).

Useful advice! Thanks!

@bombarie

bombarie commented Nov 27, 2022 •

Copy link
Copy Markdown

I would suggest changing Random.Range (0, 1) to Random.Range (0f, 1f).

Came here to say this too. It MUST be Random.Range(0f, 1f) since using ints in Random.Range excludes the last item. So in fact here it would always equate to return min + 0 * (max - min); which just returns min.

@developerJuiceLozzoc

Copy link
Copy Markdown

I came here investigating vectors in my swiftui SceneKit app. of course the majority of the content I need is implemented on some GIST by a unity C# dev. oh well. thanks

@Ashwinning

Copy link
Copy Markdown
Author

Thanks everyone for using the gist and taking the time to comment.
Thanks for catching the Random.Range overload issue @Kyonru, and thanks for the explanation @bombarie!

Can't remember - this was more than 10 years ago now, but I'm sure these issues were fixed in our codebase at the time by better programmers than me.

I'm several years late to the party in fixing the highlighted bug, AI won't make such silly mistakes anymore, and humans won't need this gist.

It was fund coding by hand while it lasted!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment