6

I need to initialize an array of characters with 1000 random characters between [a,..z] and [A,..,Z].

I don’t want do this by first generating only characters between [a,..z] and then only characters in [A...Z] but treat all 52 characters equally.

I know one way to do this is to generate a random number between 0 and 51 and assign it one of the character values.

How would I approach this problem or assign values to the random numbers between 0 and 51?

1
  • I have an array of size 1000 that holds upper case letters and lower case letters of the entire alphabet. How would i print the total number of lower case letters generated and the total number of upper case letters generated? ALSO A count of how many of each letter were generated (showing the letter and how many were generated) as well as the percent of the total that letter represents. Commented Sep 10, 2015 at 6:36

6 Answers 6

5

You have got the interesting code idea. Here might be the thought.

  1. Take all the a-z and A-Z and store them in an array[].
  2. Randomly generate a number between 1-52 (use API classes for that).
  3. You will get a number in step 2, take it as an array index and pick this index from array[] of chars.
  4. Place that picked char to your desired location/format............
  5. Process it.

Best Regards.

Sign up to request clarification or add additional context in comments.

2 Comments

i filled an array of size 51 and populated it with ascii numbers for the capital and lower case letters , i then used the random generator on my first array to fill my main array of size 1000 how does that sound
the first phase is to get a random char within the range 1-52(a-z, A-Z), when you get a char then fill it to your 1000 sized array index within a loop.
1

Let me give you some general guidelines. The "one way to do this" you mentioned works. I recommend using a HashMap<E>. You can read more about hashmaps in the docs:

http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

Alternatively, you can use another array that contains a - z and A - Z and you can use the index number to refer to them. But in this case I think it makes more sense to use a HashMap<E>.

I think you know how to create a Random object. But if you don't, check out the docs:

http://docs.oracle.com/javase/7/docs/api/java/util/Random.html

So you basically use the nextInt method of the Random class to generate a random number. And you can put that random number into your HashMap or array. And then you just put that character you get into an array that stores the result of the program.

4 Comments

Isn't a hashset a bad idea if you want to access a lot random values ?
But I guess for a beginner you should care less about performance. In this case, it makes the most sense to store the stuff I key value pairs
Well, I have never worked with HashSets, but they don't seem to have retrieve/get method that would accept a random value. So how would one use a hashset to create random outcomes en mass? I know you could use a HashMap, just like an assoziative array. But hashset ?
Sorry! @HopefullyHelpful I actually mean HashMap. Now I finally realize my silly mistake!
0

This is the sample work, hope this will work for you

import java.util.ArrayList;
import java.util.Random;

public class RandomNumber {

    public static void main(String args[]) {
        char c;
        ArrayList<Character> character = new ArrayList<Character>();
        Random rn = new Random();
        for (int i = 0; i < 500; ++i) {

            character.add((char) (rn.nextInt(26) + 66));
            character.add((char) (rn.nextInt(26) + 97));
        }

        System.out.println(character);
    }
}

1 Comment

This is not what the OP asked for. He/she doesn't want to generate a - z and A - Z seperately
0

The simplest approach would be:

// constant declared in the class
static final int LETTERS_COUNT = 'z'-'a'+1;

char[] randomChars = new char[500];
Random r = new Random();
for(int i=0; i<randomChars.length; i++) {
    randomChars[i] = (char)(r.nextInt(LETTERS_COUNT) + (r.nextBoolean() ? 'a' : 'A'));
}

If you don't like accessing random number generator two times, you can generate numbers from 0 to 51:

for(int i=0; i<randomChars.length; i++) {
    int num = r.nextInt(LETTERS_COUNT*2);
    randomChars[i] = (char)(num >= LETTERS_COUNT ? 'a'+(num-LETTERS_COUNT) : 'A'+num);
}

However I don't see any advantages of this approach. Internally r.nextInt may also change its internal state several times. The distribution should be similar in both cases.

Comments

0

You can this with a function using either arrays or arithemtic. Note that you can calculate with characters like with numbers, because they are stored as numbers (http://www.asciitable.com/), you will also note that digits, small letters and big letters are stored in sequence so that accessing ranges is pretty easy with a simple for-loop.

private Random r = new Random();
public char randomLetter() {
    int randomNumber = this.r.nextInt(52);
    if(randomNumber >= 26) {
        return (char)(('A'+randomNumber)-26);
    } else {
        return (char)('a'+randomNumber);
    }
}

The version using an array will be faster, but can only be used if the set of possible outcomes is small enough to be stored.

private Random r = new Random();
private static char[] set = new char[52]; //static because we only need 1 overall
static { //this is a static "constructor"
    for(int i = 0; i < 26; i++) {
        set[i] = (char)('a'+i);
        set[i+26] = (char)('A'+i);
    }
}

public char fastRandomLetter() {
    final int randomNumber = this.r.nextInt(52);
    return set[randomNumber];
}

Comments

0
public class ShuffleArray {

    public static void main(String[] args) {
        
        int[] array = { 1, 2, 3, 4, 5, 6, 7 };
        
        Random rand = new Random();
        
        for (int i = 0; i < array.length; i++) {
            int randomIndexToSwap = rand.nextInt(array.length);
            int temp = array[randomIndexToSwap];
            array[randomIndexToSwap] = array[i];
            array[i] = temp;
        }
        System.out.println(Arrays.toString(array));
    }
}

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.