vendredi 17 avril 2020

How to print many random lists at the same time

I need to generate a random list of letters, and then I need to list another different list.

I am using the following code, and every time I call the method it gives me the same list in one run..How can I get different lists in one run? I tried to put the methods with 2 different names then call each one, but the resulting lists still the same.

using System; 

class GFG 
{ 
static int MAX = 26; 

// Returns a String of random alphabets of 
// length n. 
static String printRandomString(int n) 
{ 
    char []alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 
                        'h', 'i', 'j', 'k', 'l', 'm', 'n',  
                        'o', 'p', 'q', 'r', 's', 't', 'u', 
                        'v', 'w', 'x', 'y', 'z' }; 

    Random random = new Random(); 
    String res = ""; 
    for (int i = 0; i < n; i++)  
        res = res + alphabet[(int)(random.Next(0, MAX))]; 

    return res; 
} 

// Driver code 
public static void Main() 
{ 
    int n = 10; 
    Console.Write(printRandomString(n)); 
} 
} 



Aucun commentaire:

Enregistrer un commentaire