Jump to content
  • Advertisement
Sign in to follow this  
4d69636861656c

How to randomize a string variable

This topic is 820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement

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

For numbers Random,
You could also use GUID.



A GUID is not usable directly as an index into a list of strings, which is what the OP asked for.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!