Sign in to follow this  
4d69636861656c

How to randomize a string variable

Recommended Posts

C# has a built-in random number generator, many games end up with something a little more custom. 

 

In general you create a new random number generator somewhere in your code.

 

Random generator = new Random();

 

You use the generator wherever you need a source of random numbers. In this case you can use Next() which has several signatures.  Next(x) returns an integer between 0 and (x-1), so Next(100) would return a random value in the range 0-99.  Another, Next(x,y) returns an integer between x and y-1, so Next(50,100) returns a value in the range 50-99.

 

Since you're looking for an index in your list:

 

{

int randomIndex = generator.Next(mystrings.Count());

return mystrings[randomIndex];

}

Share this post


Link to post
Share on other sites
Important note: The system time in seconds is generally used to seed a new random number generator. Random number generators created within the same second will generate the same sequence of numbers.

Therefore, you shouldn’t make that “somewhere” in “create a new random number generator somewhere in your code” be immediately before its use, but rather, as a member variable initialized at load-time.

Share this post


Link to post
Share on other sites
You use the generator wherever you need a source of random numbers. In this case you can use Next() which has several signatures.  Next(x) returns an integer between 0 and (x-1), so Next(100) would return a random value in the range 0-99.  Another, Next(x,y) returns an integer between x and y-1, so Next(50,100) returns a value in the range 50-99.

 

I probably don't know enough about the libraries used or I'm missing something else, but I get this error 'Random does not contain a definition for <Next> and no extension method <Next> accepting a first argument of type <Random> could be found.'.

Share this post


Link to post
Share on other sites

Dumb question: did you "using System;" at the top of your .CS file?

 

Yes, I was using System.

There seemed to be a problem with System.Random and UnityEngine.Random. Fixed it now by specifying everywhere I used a 'Random' function which library to use.

 

Thank you all for the help.

Edited by 4d69636861656c

Share this post


Link to post
Share on other sites

 

Dumb question: did you "using System;" at the top of your .CS file?

 

Yes, I was using System.

There seemed to be a problem with System.Random and UnityEngine.Random. Fixed it now by specifying everywhere I used a 'Random' function which library to use.

 

Thank you all for the help.

 

 

Oh, if you're in Unity, I suggest just using their random, instead of mixing and using System.Random as well.  At least until you decide that you need a better random than either and roll your own =)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this