0

I am currently attempting to generate two random (whole) numbers at once for a game I am making. I am trying to randomly generate a start and end tile. When I do this though, my two randomly generated numbers are always the same. I understand this is probably just because they are being called calculated to close together, but I can't find any information that works on how to get two different random numbers. my code is quite simple (in c#):

float startTile = Mathf.Ceil(Random.Range(1.0f, 8.0f));
float endTile = Mathf.Ceil(Random.Range(1.0f, 8.0f));

Thank you in advance!

Edit

This was marked as a duplicate question, where the answer for the old question was:

Random rand = new Random();
int t = rand.next(x,x2);

Unless I'm doing something incorrect, I am unable to even use the rand.Next(x,x1) function within unity.

enter image description here

The actual code:

private void SpawnStartandEndTiles()
{
    Random rand = new Random();
    int t = rand.N //<Next is not an option

    //Other misc. Code that uses the random numbers
}

Please let me know if I am just not getting something with how the Random function works.

Thanks so much.

  • With your update, show how you declared `rand`. – Lasse V. Karlsen Dec 16 '16 at 18:27
  • I don't think this is a duplicate because you are using Unity's [`Random.Range`](https://docs.unity3d.com/ScriptReference/Random.Range.html) and not .NET's Random class. I don't know Unity, but there is a [`Random.InitState`](https://docs.unity3d.com/ScriptReference/Random.InitState.html) method you can call with an integer seed value, so a potential solution is to call `Random.InitState(new System.Random().Next())`, between `Random.Range` calls. But relying on .NET's random to generate a random seed to then generate another random number seems weird.. maybe just use .NET's random by itself. – Quantic Dec 16 '16 at 18:42
  • 2
    Clarifying that you are using `UnityEngine.Random` and not `System.Random` may help get the question reopened. – Quantic Dec 16 '16 at 18:48
  • I need to clarify my previous comment which is currently incorrect. As the linked duplicate shows, you need to create only one `new System.Random()`, then call `.Next()` on that single instantiation. If you try my suggestion, do NOT create a `new Random()` every time or else you'll probably have the same exact problem. My previous comment should be, `Random.InitState(yourRandomInstantiation.Next())`, where `var yourRandomInstantiation = new System.Random()` is called only once. – Quantic Dec 16 '16 at 19:02
  • 1
    @Joey you should never be calling `new Random()` in unity. Add `using UnityEngine;` to the top of your file and just do `int t = Random.Next(1.0f, 8.0f)` If that code is giving duplicates you have errors elsewhere in your code that you have not shown s. – Scott Chamberlain Dec 16 '16 at 19:59

1 Answers1

0

First of all don't use System.Random because using most classes in System namespace (*) drastically increase the size of your game. Instead use UnityEngine.Random

Random numbers are always generated using a seed. Given a specific seed the sequence of random numbers are always the same. You can say computed random is never really random.

By setting the Random.seed to a value depending on the time makes Random.Range to generate numbers close to real random.

For example:

Random.seed = System.DateTime.Now.Millisecond;
//...
float startTile = Mathf.Ceil(Random.Range(1.0f, 8.0f));
float endTile = Mathf.Ceil(Random.Range(1.0f, 8.0f));

(*) These are in mscorlib.dll and therefore does not have any impact on the size of game

System.Collection.*
System.Collection.Generic.*
System.IO.*
Bizhan
  • 16,157
  • 9
  • 63
  • 101