1 2 Previous Next 17 Replies Latest reply: Nov 21, 2008 12:16 AM by 843785 RSS

    Random character generator

    843785
      What method could i use to generate two random characters
      example i want to generate a random character between . and #
        • 1. Re: Random character generator
          843785
          Look at the decimal values for characters in the ASCII table.

          Then get an int in the right range and cast to char.

          Or make an array of chars you want and get a random index.
          • 2. Re: Random character generator
            843785
            robertpurpose wrote:
            What method could i use to generate two random characters
            example i want to generate a random character between . and #
            1. Add all characters to an array.

            2. Generate a random number within the size of the array.

            3. Use the random number to get the character.
            • 3. Re: Random character generator
              843785
              overrule wrote:
              1. Add all characters to an array.
              2. Generate a random number within the size of the array.
              3. Use the random number to get the character.
              overrule wrote:If you don't know, don't answer. Simple as that.
              • 4. Re: Random character generator
                843785
                TuringPest wrote:
                overrule wrote:
                1. Add all characters to an array.
                2. Generate a random number within the size of the array.
                3. Use the random number to get the character.
                overrule wrote:If you don't know, don't answer. Simple as that.
                I'd be happy to answer if you came up with a proper post.
                • 5. Re: Random character generator
                  843785
                  Here you go.
                  public static char randChar(char beginChar, char endChar)
                  {
                           java.util.Random rand = new java.util.Random();
                           return (char)(rand.nextInt((int)endChar - (int)beginChar) + beginChar);
                  }
                  • 6. Re: Random character generator
                    843785
                    If you run this code on JVM 1.4 your function isn't random at all ... try it! :)

                       public static char randChar(char beginChar, char endChar) {
                            java.util.Random rand = new java.util.Random();
                            return (char) (rand.nextInt((int) endChar - (int) beginChar) + beginChar);
                        }
                    
                        public static void main(String[] args) {
                            for (int i = 0; i < 10; i++) {
                                System.out.print(randChar('a', 'z'));
                            }
                        }
                    p..s it seems that Sun has changed the behaviour of the Random() constructor in JVM 1.6 (or perhaps in JVM 1.5)
                    • 7. Re: Random character generator
                      843785
                      i did something close but it doesnt work here is the sample code:
                      Random r=new Random();
                      
                      char[] check=new char[2];
                      
                      arrayIndex= r.nextInt(1);
                      
                      for (int i=0; i< otherarray.length; i++)
                      {
                          otherarray=check[arrayIndex];
                      }
                      the problem i have with this code is that it doesnt choose randomly between index 0 and 1 or check[] it just prints 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                      • 8. Re: Random character generator
                        843785
                        robertpurpose wrote:
                        i did something close but it doesnt work here is the sample code:
                        Random r=new Random();
                        
                        char[] check=new char[2];
                        
                        arrayIndex= r.nextInt(1);
                        
                        for (int i=0; i< otherarray.length; i++)
                        {
                        otherarray=check[arrayIndex];
                        }
                        the problem i have with this code is that it doesnt choose randomly between index 0 and 1 or check[] it just prints 0
                        Read the documentation for nextInt, then answer this question; "what possible values can r.nextInt(1) return?"
                        • 9. Re: Random character generator
                          843785
                          from what i reasd online i think it should b generating a random value between 0 and 1, i may be incorrect
                          • 10. Re: Random character generator
                            843785
                            robertpurpose wrote:
                            from what i reasd online i think it should b generating a random value between 0 and 1, i may be incorrect
                            You are:

                            <quote>
                            Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)
                            </quote>

                            A value v in the range from lo (inclusive) to hi (exclusive) satisfies:
                            lo <= v < hi
                            • 11. Re: Random character generator
                              843785
                              so what could i use in place of this?
                              • 12. Re: Random character generator
                                843785
                                robertpurpose wrote:
                                so what could i use in place of this?
                                Do you know the fable about giving a man to fish versus teaching him how to fish?
                                • 13. Re: Random character generator
                                  843785
                                  ok ill experiment with the math.random()
                                  • 14. Re: Random character generator
                                    843785
                                    Or read the API. Learning how to fish the answers from the API is a good skill, and really not that hard to do. In this case the asnwer is made really obvious in the random API.

                                    JSG
                                    1 2 Previous Next