Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using System.Random

Tags:

c#

random

integer

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?

like image 600
Ishkur Avatar asked Aug 03 '12 04:08

Ishkur


2 Answers

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

like image 57
Kevin Avatar answered Sep 29 '22 07:09

Kevin


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();
like image 43
Simon Whitehead Avatar answered Sep 29 '22 07:09

Simon Whitehead