Random Number Generation

Error CS1061, dont know why its occuring

Page 1 of 1

9 Replies - 2265 Views - Last Post: 26 August 2010 - 07:11 AM Rate Topic: -----

#1 Guest_Sean*


Reputation:

Random Number Generation

Posted 25 August 2010 - 02:18 PM

Hi Everyone,

I'm new to programming and trying to create a simple console program that generates a random number between a range (in this case 1 and 15). I've looked on the internet for directions on using the Random class, but I am getting an error that I cannot understand. I wouldn't really post this otherwise (it seems basic) but I can't find anything on the internet that explains it very well.

using System;

public class Random
{
	private int rNumber(int min, int max)
	{
		Random rGen = new Random();
		return random.Next(1,15);
	}


	public static void Main();
	{
		Console.WriteLine("{0}", rNumber);
	}
}



The compiler doesn't recognize "Next" as a method, which is why I tried using class Random -- it doesnt seem to recognize this Next function. The error is CS1061 - This error occurs when you try to call a method or access a class member that does not exist.

It was my understanding that the Random class is part of the System namespace, and I've called System. Is there another namespace I need to call in so that the "Next" function is recognized?

Is This A Good Question/Topic? 0

Replies To: Random Number Generation

#2 Guest_Guest*


Reputation:

Re: Random Number Generation

Posted 25 August 2010 - 02:21 PM

it should be rGen.Next(1,15)
Was This Post Helpful? 0

#3 Guest_Guest*


Reputation:

Re: Random Number Generation

Posted 25 August 2010 - 02:25 PM

Yeah, that actually isn't causing the error though. I was just changing the name b/c I prefer rGen.

Thanks for pointing that out though.
Was This Post Helpful? 0

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5949
  • View blog
  • Posts: 23,207
  • Joined: 23-August 08

Re: Random Number Generation

Posted 25 August 2010 - 02:30 PM

Your class is called Random, so it's looking for a method called Next in YOUR class, not the .NET class.
Was This Post Helpful? 2
  • +
  • -

#5 Guest_Guest*


Reputation:

Re: Random Number Generation

Posted 25 August 2010 - 02:41 PM

View PostJackOfAllTrades, on 25 August 2010 - 01:30 PM, said:

Your class is called Random, so it's looking for a method called Next in YOUR class, not the .NET class.


That makes sense, but how do I resolve this?

Changing the name of the class doesnt help - it still wont compile.
Was This Post Helpful? 0

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5949
  • View blog
  • Posts: 23,207
  • Joined: 23-August 08

Re: Random Number Generation

Posted 25 August 2010 - 03:01 PM

There are a few things wrong with this.

using System;

namespace MyNamespace
{ 
    public class MyRandom
    {
        // Where this uses no member variables, it can be declared static
        private static int rNumber(int min, int max)
        {
            Random rGen = new Random();
            return rGen.Next(min, max); // I assume you want to use what's passed in
        }


        public static void Main() // You had a stray ; here
        {
            // Where this is a static method call you don't need an object reference
            Console.WriteLine("{0}", rNumber(1, 15));
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#7 logicandchaos  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 24-January 10

Re: Random Number Generation

Posted 25 August 2010 - 03:10 PM

this won't solve the problem but shouldn't return random.Next(1,15); be return random.Next(min,max); otherwise min and max aren't used at all and actually it should be: return rGen.Next(min,max); i just tried:
Random rGen = new Random();
            //int num;
            //num=rGen.Next(1, 15);



sorry i hit the button by mistake :S here is the code i just tried in my console app:
Random rGen = new Random();  
int num;  
num=rGen.Next(1, 15); 


works fine
Was This Post Helpful? 0
  • +
  • -

#8 Guest_Guest*


Reputation:

Re: Random Number Generation

Posted 25 August 2010 - 03:26 PM

Thanks guys.

So in the Main() method, the int range (1,15) is passed to the rGen.Next method via the code argument:

rNumber(int min, int max)

Is that right?

I could also specify a range in the method itself and it would be restricted to that range, I think?
Was This Post Helpful? 0

#9 Guest_Sean*


Reputation:

Re: Random Number Generation

Posted 25 August 2010 - 04:51 PM

Well I've got it working and I applied to a simple game where you choose a strategy and get back, a win, tie, or loss. Using a multidimensional array.

Problem is that when you run the program (it works) it really doesnt seem random at all. Tends to stuck in going the same way over and over (though it does make changes, but never seems to hit certain values), any ideas why? Maybe the Random class in .NET is very random? I read somewhere that might be the case. Heres my code:

using System;

namespace MyNamespace
{ 
    public class MyRandom
    {
        private static int rNumber(int min, int max)
        {
            Random rGen = new Random();
            return rGen.Next(min, max); // I assume you want to use what's passed in
        }


        public static void Main() 
        {
		string[,] outcomes = { {"Tie","Win", "Win"}, {"Lose", "Tie", "Win"}, {"Lose", "Lose", "Tie"}, {"Lose", "Lose", "Lose"} };
		int stayOpen = 1;

		while (stayOpen < 5)
		{

			Console.WriteLine("Choose a strategy: 0, 1, 2, or 3."); // Zero is the best strategy
			string myInput = Console.ReadLine();
        		int myInt = Int32.Parse(myInput);

			int nOne = rNumber(0,3); // This isnt being used but left it in anyway
			int nTwo = rNumber(0,3); // This random "nTwo" value seems to get stuck and not really generate randomness well

			if (myInt == 0)
			{
				Console.WriteLine("{0} at position [0,{1}]", outcomes[0, nTwo], nTwo);
				Console.ReadLine();
			}
			else if (myInt == 1)
			{
				Console.WriteLine("{0} at position [1,{1}]", outcomes[1, nTwo], nTwo);
				Console.ReadLine();
			}
			else if (myInt == 2)		
			{	
				Console.WriteLine("{0} at position [2,{1}]", outcomes[2, nTwo], nTwo);
				Console.ReadLine();
			}
			else if (myInt == 3)
			{	
				Console.WriteLine("{0} at position [3,{1}]", outcomes[3, nTwo], nTwo);
				Console.ReadLine();
			}

		}
        }
    }
}





The Random class clearly creates some variation, but it hardly seems random. Try it out.
Was This Post Helpful? 0

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,456
  • Joined: 08-June 10

Re: Random Number Generation

Posted 26 August 2010 - 07:11 AM

View PostSean, on 25 August 2010 - 10:51 PM, said:

The Random class clearly creates some variation, but it hardly seems random. Try it out.


The Random class is not a true random generator, but a Pseudorandom Number Generator. Random is seeded with the system clock by default. Meaning that if you create two Random objects in a small enough timeframe, you'll get the same sequence of numbers from it. And every time you call your method, you create a new random. But computers are fast enough that this is happening within the same millisecond, so the seed value is the same.


The solution is to make one single Random global to your class, instantiate it at the beginning, and then use the single instance for all your random numbers.

Also, remember that the lower bound is inclusive, but the upper bound is exclusive. Meaning that if I did rand.Next(1, 4), the possible outcomes are 1, 2, and 3. Not 4.

This post has been edited by insertAlias: 26 August 2010 - 07:13 AM

Was This Post Helpful? 3
  • +
  • -

Page 1 of 1