Java random array generator

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

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

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

How can I approach this problem or assign values ​​to random numbers from 0 to 51?

+6
source share
5 answers

You have an interesting code idea. There may be a thought.

  • Take all az and AZ and save them in the [] array.
  • Arbitrary number creation between 1-52 (for this, use the API classes).
  • You will get the number in step 2, take it as the index of the array and select this index from array[] characters.
  • The place that char selected in the right place / format ............
  • Process it.

Best wishes.

+5
source

Let me give you some general recommendations. "One way to do this," you mentioned work. I recommend using HashMap<E> . You can learn more about hashmaps in the docs:

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

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

I think you know how to create a Random object. But if you do not, check 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 this random number in your HashMap or array. And then you just put this symbol in the array in which the result of the program is stored.

+1
source

This is an example of work, hope this works 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); } } 
0
source

The easiest way:

 // 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 the random number generator twice, 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 do not see any advantages of this approach. Internally, r.nextInt can also change its internal state several times. The distribution should be the same in both cases.

0
source

You can use this function using either arrays or arithmetic. Please note that you can count with characters as with numbers, because they are stored as numbers ( http://www.asciitable.com/ ), you will also notice that numbers, small letters and capital letters are stored in sequence, so access to ranges is pretty simple with a simple 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 the array will be faster, but can only be used if the set of possible results is small enough for storage.

 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]; } 
0
source

All Articles