Random.range not working


I tried to include

using System.Random;

in my C# script, and the compiler is telling me that: 'System.Random' is a type not a namespace. A using namespace directive can only be applied to namespaces. What am I doing wrong? I need to be able to use the function: Random.Range(n, k);

As the error says, System.Random isn't a Namespace, so you don't put using on it. What you want is Unity's Random.Range function. This code will work:

void Start () {
    float x = Random.Range(1, 10);

If you are using the .Net System namespace, you would need to clarify to the compiler, which Random you want:

using System;

void Start () {
    float x = UnityEngine.Random.Range(1, 10);

Also, another excellent Q/A to read about random numbers, is How do I Generate a Random Number?

Update: for PC/C# users, yet another reason to use C# Visual Studio Express - it will show you namespace conflicts. When I entered Random.Range() into my code, it told me there was a conflict between .Net's System.Random and UnityEngine's Random, so I knew I needed to prepend UnityEngine to my function call.

Random.Range is not part of System.Random, it's a Unity function. Remove the "using System.Random;" line.