RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

"Random" question

Spudz777

Sorceror
"Random" question

Okay, I thought I had random number generation under control, but a typo in a script threw me for a loop... What does
Code:
Utility.Random(int a, int b);
return? It's not a number between a and b, that would be:
Code:
Utility.RandomMinMax(int a, int b);
So...?

*edit*Post 100! (took me long enough)...
 

Lysdexic

Sorceror
They are the exact same, just a different way to call upon it. Using a search through the scripts would of pulled a few examples of both out and told you that. (If you have XP and "A word or phrase in a file" isn't working, open Regedit, and under HKEY_CLASSES_ROOT\.cs\ add a new Key, name it PersistentHandler, then change the default value to {5e941d80-bf96-11cd-b579-08002b30bfeb}. Once done with that, reboot and it will search within the file now).
 

UOT

Knight
Code:
Utility.Random( a, b )
Returns a value between a and a+b so if you have a = 5 and b = 100, it'll return a number between 5 and 105 while the same numbers using
Code:
Utility.RandomMinMax(a,b)
Returns a number between a and b so between 5 and 100 going by the example above. So they work a bit differently.
 

Lysdexic

Sorceror
Dices.cs:
Code:
this.PublicOverheadMessage( MessageType.Regular, 0, false, string.Format( "*{0} rolls {1}, {2}*", from.Name, Utility.Random( 1, 6 ), Utility.Random( 1, 6 ) ) );

Your use of Utility.Random would mean it returns a value between 1 and (1+6).
 

Spudz777

Sorceror
UOT, you're almost right... Having played with it for a while, I believe it returns a+Utility.Random( b ), so the range of possible outcomes is the set [a,a+b-1]. So the dice script returns [1,1+6-1] = [1,6], which makes sense.

I was just getting some strange results from a script where I expected Utility.Random(5,10) to return a value between 5 and 10, but it only worked about half the time (because the other half I was getting 11-14). Try it out Lysdexic, they're not the same...
 

UOT

Knight
Actually taking a better look at the core
Code:
		public static int Random( int from, int count )
		{
			if ( count == 0 )
			{
				return from;
			}
			else if ( count > 0 )
			{
				return from + m_Random.Next( count );
			}
			else
			{
				return from - m_Random.Next( -count );
			}
		}
It would take 1 + (0 thru 5) because Random.Next(6) would return a number between 0 and 5.
 
Top