Using System.Random

24,707

Solution 1

Random is a class in the System namespace. Change the first line to just using System; and you should be good to go.

Solution 2

The Random class is a part of the System namespace, not System.Random. You can reference the type directly using the namespace though:

System.Random rnd = new System.Random();

Or..

using System;

Random rnd = new Random();

Solution 3

You only have to use the System-Namespace

using System;

int randomNumber;
Random RNG = new Random();
randomNumber = RNG.Next(1,10);

Solution 4

You don't need the using statement. Your using statement is invalid.

Random is a class in System namespace. Simply use

using System;

instead of using System.Random;

Share:
24,707
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm using C# in .NET Framework 3.5 and am trying to generate a random integer by using Random(). My code is here:

    using System.Random;
    
    int randomNumber;
    Random RNG = new Random();
    randomNumber = RNG.Next(1,10);
    

    I think everything should be alright, but I'm getting the error that System.Random isn't a valid namespace, but I'm pretty sure it is...

    Anybody know what's the problem or some other method I should be using to generate a random integer within a range?