Click here to Skip to main content
15,885,889 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have been trying to write a method that generates random letters between two given char values. So far i believe the best way is to convert the char characters to ascii values then compare them with the user input and make a for loop between those 2 values. My problem is i dont know how to compare the values after they have been converted here is my code so far:
Java
public void add (char fromChar, char toChar){
        Random a = new Random();
       char[] characters = { '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'};
       int start =65;
       for(int i =0; i<characters.length;>         characters[i] = start;
         start++;
         
         
       }     
       for(int i = fromChar; i<tochar;>           System.out.println(a.nextInt(fromChar,toChar);
       }
        
    }


[edit]Spurious bold removed, Code block added - OriginalGriff[/edit]
Posted
Updated 17-Nov-12 20:33pm
v2

1 solution

You need to set a seed based on some arbitrary value, then call the nextInt() method with a limiting range. Something like:

Java
int range = toChar - fromChar;
int seed = fromChar * toChar;
Random rr = new Random(seed);
for (int i = 0; i < 10; ++i) {
    char randomChar = (char)(fromChar + rr.nextInt(range));
    System.out.println("Next character: " + randomChar);
}
 
Share this answer
 
Comments
diego14567 18-Nov-12 8:27am    
thanks man i never would have thought about doing it this way
Richard MacCutchan 18-Nov-12 8:34am    
You're welcome.

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