# Un-Randomize Random.insideunitSphere

**URL:** <https://discussions.unity.com/t/un-randomize-random-insideunitsphere/654618>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [February 7, 2017, 1:49pm UTC](https://discussions.unity.com/t/un-randomize-random-insideunitsphere/654618 "2017-02-07T13:49:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CrazyCookieBuddy](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@CrazyCookieBuddy](https://discussions.unity.com/u/CrazyCookieBuddy)\
**Post date:** [February 7, 2017, 1:49pm UTC](https://discussions.unity.com/t/un-randomize-random-insideunitsphere/654618/1 "2017-02-07T13:49:11Z")

</div>

I just wrote my first code for randomising and I’m using seeds to save data.  
Now I have the problem that i could spawn random objects inside a boundary of x, y and z, but that is shaped like a cube.  
Since I wrote a code to convert the inputted seed into a randomizer, is there a way to use that randomising in “insideUnitSphere” to make the boundaries a sphere?  
Maybe something like this (which doesn’t work) :

```csharp
//Defining a string seed beforehand
System.Random myRandom = new System.Random(seed.GetHashCode());
Vector3 pos = new Vector3(myRandom.insideUnitSphere);

```

Because otherwise, the Random:insideUnitSphere is **completely** random, which defies the usage of a seed…

---

<div class="post-metadata">

**Author:** ![JoeStrout](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/joestrout/32/200738_2.png) [@JoeStrout](https://discussions.unity.com/u/JoeStrout)\
**Post date:** [February 7, 2017, 3:25pm UTC](https://discussions.unity.com/t/un-randomize-random-insideunitsphere/654618/2 "2017-02-07T15:25:20Z")

</div>

> [@CrazyCookieBuddy](#):
>
> Because otherwise, the Random:insideUnitSphere is **completely** random, which defies the usage of a seed…

I don’t believe you. 😛 If you’re talking about Unity’s RNG, you can save, restore, or initialize its [state](https://docs.unity3d.com/ScriptReference/Random-state.html). With System.Random, I guess your gripe is that it doesn’t have an insideUnitSphere method, but you could make one [this way](http://math.stackexchange.com/questions/87230).

---

<div class="post-metadata">

**Author:** ![takatok](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/takatok/32/586449_2.png) [@takatok](https://discussions.unity.com/u/takatok)\
**Post date:** [February 7, 2017, 6:57pm UTC](https://discussions.unity.com/t/un-randomize-random-insideunitsphere/654618/3 "2017-02-07T18:57:32Z")

</div>

I believe your running into the problem that there are 2 different Random classes available to you:  
System.Random which is the .Net built in Random Class  
UnityEngine.Random which is Unity’s Random Class

Now Sytem.Random does take a constructor that has a seed (like your example), Unity Engine’s random does not it does it like this:

```csharp
UnityEngine.Random myRandom = new UnityEngine.Random();
myRandom.InitState(seed);

```

One Quick note, you can avoid having to type out System.Random or UnityEngine.Random to make sure your getting the right one with this line at the top with your using statements:

```csharp
using Random = UnityEngine.Random;

```

Another thing to note, is that Random.insideUnitCircle is a static member of the random class. You can’t access it like this:

```csharp
// This is wrong and will throw a compile error
Random myRand = new Random();
Vector3 pos = myRand.insideUnitSphere;

```

You have to access it like this:

```csharp
Vector3 pos = Random.insideUnitSphere;

```

However you can still initialize this static Random instance like this (so you always get the same points each time you run your code:

```csharp
using Random = UnityEngine.Random;

public int seed;

void Awake()
{
         Random.InitState(seed);
}

void Start()
{
       //This will generate the same Vector3 everytime you hit Play
       Vector3 pos = Random.unitInsideSphere;
}

```

Edit: if your using seeds to test your code , I would highly suggest just using the static Random instance. That way one script can call the InitState in its Awake function, than all the other scripts can just call Random.Range(). This way all you scripts can use one copy of random and it will be easy to duplicate results. If you try to instantiate your own copies of random it might be harder to debug and sync them.

---

<div class="post-metadata">

**Author:** ![CrazyCookieBuddy](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@CrazyCookieBuddy](https://discussions.unity.com/u/CrazyCookieBuddy)\
**Post date:** [February 8, 2017, 9:57am UTC](https://discussions.unity.com/t/un-randomize-random-insideunitsphere/654618/4 "2017-02-08T09:57:17Z")

</div>

Perfect!!!

Thanks, and sorry for the stupid questions, I just haven’t had found the Random.InitState yet.  
This makes a lot **so much** easier ❤
