Skip to main content
gamedev.net
🔒 Locked

Using Random

Started by foolios Aug 5, 2007 at 4:39 PM 4 replies 1.7k views
Original Post
foolios
foolios
I want to create a random number based on a range, but for some reason it isn't working. http://msdn2.microsoft.com/en-us/library/2dx6wyd4.aspx I tried to figure this out from some examples in the MSDN. It appears there is a NEXT method that can be used with random. The intellisense tells me that I can use a minimum range and a maximum range. Take a look please:

            int r = h * w;
            Room[] rooms = new Room[r];
            Random rm = new Random();
            rm.Next(0, r);
            Console.WriteLine(rm);

Instead of the random number, the text System.Random is what's printed to the screen.
I rate users. Its just not having much impact as my rating is low...
foolios
foolios
Ok, somehow I realized that I needed to add the line:

int newrm = rm.Next(0, r);

so that the newrm now has this new random number.
But I don't understand why I had to do this. rm was already an object. Why did I need to assign rm.Next() to a variable?
Thanks in advance.
I rate users. Its just not having much impact as my rating is low...
foolios
foolios
Ok, after looking it over a couple time, I see that .Next is a method of the random class and in that code, the method is being run from the new rm object. It has a return value and I must assign it to something if I want to use it.
I rate users. Its just not having much impact as my rating is low...
foolios
foolios
Quote:
Original post by Telastyn
Random.Next(...) returns the random number. The class Random represents a random number generator, not a random number itself.


Thank you
I rate users. Its just not having much impact as my rating is low...
borto
borto
You also don't need to set the minimum range if it's going to be 0. Random.Next with one parameter will return a range from zero to one below that number.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.