Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java-Randomly generate 100 chars?

Options
  • 04-08-2004 2:52pm
    #1
    Closed Accounts Posts: 1,959 ✭✭✭


    Hi all,
    I'm writing a program where I have to het the computer to randomly generate 100 chars in the range A to Z, how do I do this? I've tried a few websites but no luck.
    Cheers!


Comments

  • Registered Users Posts: 1,771 ✭✭✭jebuz


    wel what u cud do is initialise a char array of size 26 and then input all the letters a - z
    then use something like this to generate the 100 chars...

    Random generator = new Random();
    int ranNum = 0;

    for(int i = 0; i < 100; i++)
    {
    ranNum = generator.nextInt(26);
    System.out.println("Random Char " + (i + 1) + " is " + arrayName[ranNum]);
    }

    and that should give u an output like...

    Random Char 1 is A
    Random Char 2 is P
    Random Char 3 is W.... etc


  • Closed Accounts Posts: 47 PhilH


    wouldn't it be a bit simpler to do

    Random generator = new Random();
    for(int i = 0; i < 100; i++)
    {
    char ch = (char) ('a' + generator.nextInt(26));
    System.out.println(ch);
    }


    PHiL


Advertisement