Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi,

I have basically a list of string variables in the order of this below. What i am trying to do is basically randomise one of these strings with a method or something, i have heard of some randomise methods but nothing to do with this.

C#
String a;
String b;
String c;
String d;
String e;
String f;
String g;
String h;
String i;


thanks, and i look forward to hearing back from one of you guys.
Posted
Comments
Sergey Alexandrovich Kryukov 27-Jan-14 14:07pm    
First of all, this is not a list. :-)
—SA
Matt T Heffron 28-Jan-14 13:15pm    
This is not clearly defined.
You have 9 independent string variables.
Do you want to:
1) Choose the value of one of these variables at random and then do something with that string, or
2) Use all 9 values of the string variables in a random permutation
Possibly doing either of the above in a loop.
(Note that doing #1 9 times in a loop is not the same as #2, it may not return all 9 values, some may be returned multiple times.)
Please use "Improve question" above to add clarification.

Create an array where index 0 is for a and 8 is for i. Now randomize a number between 0 to 8 (http://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx[^]). Pick the variable at the new index...
 
Share this answer
 
You can use random class for this purpose
Look at the code below, its a little crude, but should give you a basic idea:
C#
string[] s = new string[] {"a","b","c","d","e","f","g","h","i" };
           Random rnd = new Random();

           int i = 0;
           do
           {
               Console.WriteLine("Hit a key .....");
               Console.ReadLine();
               Console.WriteLine(s[rnd.Next(s.Length)]);

           } while (i < s.Length);

Look at this code also, i have tried another scenario:
C#
string s = "abcdefghi";

           Random rnd = new Random();
           int i = 0;
           do
           {
               Console.WriteLine("Hit a key .....");
               Console.ReadLine();
               Console.WriteLine(s.Substring(rnd.Next(s.Length),1));
                i++;

           } while (i < s.Length);



In the first code i have taken a string[] and in the next code i have taken a single string:

[Agent_Spock]

Basically, random class is used to get a random number between any 2 numbers as shown in the above example.

Thanks
 
Share this answer
 
v5
Comments
hgh6484 27-Jan-14 14:28pm    
S is a string array, you don't need to use '.ToString()' method in 'Console.WriteLine'.
Rahul VB 27-Jan-14 14:31pm    
I am sorry brother, my bad. I have this bad habit of writing ToString() every where. :). Solution improved again.
hgh6484 27-Jan-14 14:42pm    
now it's perfect answer.
Rahul VB 27-Jan-14 14:45pm    
Thanks brother
Sergey Alexandrovich Kryukov 27-Jan-14 15:55pm    
Very redundant code, in terms of the object "s". It could be just one string s = "abcdef...", but it should better be just integer from, say, 0x21 to some maximum, converted to char code point. Besides, your code creates only one-character strings. OP did not explain what's expected for "random" string, but I cannot believe that one-character string is assumed...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900