0

I have used C++ for a long time. Now, I just started working with C# in Unity3D. I'm almost complete at finishing an app but I can't seem to find a working int generator in Unity C# (With Min and Max values). I've tried the following:

r = Random.Range(min, max)

and

Random rand = new Random();
r = rand.next(min, max)

Both didn't work. I know in C++ you use:

r = rand() % max + min;

If you want a random number. But what is the case with Unity C#? Code:

EDIT:

using UnityEngine;
using System.Collections;

public class STarget : MonoBehaviour {
public GameObject inter;
int rand;
public GameObject TS1;
public GameObject TS2;
public GameObject TS3;
public GameObject TS4;
public GameObject TS5;
void Update () {
    rand = Random.Range(1, 6);
}
public void GameRun () {
    switch (rand){
        case 1:
            inter = TS1;
            break;
        case 2:
            inter = TS2;
            break;
        case 3:
            inter = TS3;
            break;
        case 4:
            inter = TS4;
            break;
        case 5:
            inter = TS5;
            break;
    }
    Game.TargetSingle(inter);
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • 6
    Explain "didn't work", you got a exception? you got a result you where not expecting? We can't help you sove a problem if you don't descibe the problem. – Scott Chamberlain May 08 '16 at 23:24
  • 1
    `rand.Next(min,max)` checks out. Make sure you don't fall into [the hole of new-ing identically seeded randoms in a loop](http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) – Nathan Cooper May 08 '16 at 23:25
  • `Random.Range(min,max)` should also work. Depending on what you are wanting. – Danny Herbert May 08 '16 at 23:27
  • rand.Next doesn't check out when you are using Unity. Keep in mind, I'm not talking about normal C#. –  May 09 '16 at 00:52
  • Just so you know, rand() % max + min is a really bad way to get a random number in C++. You are skewing the distribution doing this. Furthermore, there are much better methods in C++ to generate random numbers - see [Pseudo-random number generation](http://en.cppreference.com/w/cpp/numeric/random) – bodangly May 09 '16 at 01:04
  • I know about that and I also use C-Time along with Rand() % Min + Max. You are not helping my question by judging my skills with a whole different coding language that I use. –  May 09 '16 at 01:07
  • 2
    your script is **TOTALLY BIZARRE**. it does not even show where you call GameRun. your use of Update() is totally bizarre. – Fattie May 09 '16 at 01:25

1 Answers1

3

Coming from C++, just like r = rand() % max + min would give you random numbers between min and max in C++,it is different in C#. First, you use Random.Range(min, max) to get random number in Unity.

Something to really understand is that Random.Range(min, max) will not really give you random from between min and max like it does in C++. It will give you a random number between min an (max-1) if the provided min and the max are integer.

So when you need random number from 1 to 5, add 1 to the max(5) or you will get only numbers from 1 to 4. Random.Range(2, 5+1) or Random.Range(min, max+1). That's probably your problem.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Remember that it includes the max value if you pass `float` to it though. – Danny Herbert May 09 '16 at 00:19
  • @gjttt1 I think I said that. "It will give you a random number between min an (max-1) if the provided min and the max are **integer**" – Programmer May 09 '16 at 00:23
  • ah yes, I must've read it too fast! My mistake – Danny Herbert May 09 '16 at 00:25
  • @gjttt1 That's fine. Happens why I read some questions and answers on SO too. – Programmer May 09 '16 at 00:27
  • I knew how it subtracts 1 from the max. My problem is that whenever I run the script, I get the same number repeated. –  May 09 '16 at 00:49
  • I just found out about pseudo-random. Anyone know how to have truly random numbers (With a Min and Max) and not pseudo-random numbers? –  May 09 '16 at 00:55
  • @AustinKugler. Did you try the exact code I posted in my answer? If yes, please put **EDIT** in your question and post your whole C# code so that I will know what exactly you are doing wrong. – Programmer May 09 '16 at 00:55
  • I'll post my code in a minute. Hope you can find the answer! –  May 09 '16 at 00:59
  • 3
    @AustinKugler Dude your code is bad. This has nothing to do with your random number function. Where are you calling GameRun () from? you are not calling it from anywhere..... Is this really your whole code? – Programmer May 09 '16 at 01:24
  • 1
    @AustinKugler Programmer is right. re-evaluating `Random.Range` in the update function just to use it in the `GameRun()` function like this is strange. Knowing when your call this function will help find the proper solution. The answer you gave is an unnecessarily convoluted way of doing this. – Danny Herbert May 09 '16 at 01:31
  • @AustinKugler as a new user, be sure to TICK any answer. it's the only way you get points when youa re new, then you avoid moderation. Cheers buddy – Fattie May 09 '16 at 03:06
  • I guess given that the question has been closed it doesn't matter much :O – Fattie May 09 '16 at 03:06