Results 1 to 5 of 5

Thread: How to get a random value from a string

  1. #1
    Join Date
    Aug 2009
    Posts
    63

    How to get a random value from a string

    I am trying something in Java and I need help from you all guys. So here is my problem:

    I have a string of type regular expression, for example: A-H or 0-8 and I'd like to get a random value in that regex. So it would give something like:
    myFunction ("0-8") returns 0 or 1 or 2 or 3 etc...
    and myFunction ("A-H") returns A or B or C or D etc...

    I need an idea how to do this effectively.

  2. #2
    Join Date
    Nov 2008
    Posts
    1,054

    Re: How to get a random value from a string

    And if you are trying something like that:

    Code:
    myfunction(String pattern){
    String[] bornes = pattern.split("-");
    Vector vect = new Vector();
    if (bornes[0].isNumber()){
    int i = bornes[0];
    while i <= bornes[1]{
    vector.add(i++);
    }else{
    }
    return vector.get(myRandom(vector.size()));
    }

  3. #3
    Join Date
    Nov 2008
    Posts
    1,221

    Re: How to get a random value from a string

    One idea would be to translate the ascii characters, then you have an interval whose two terminals are numbers. You can then use a random subject with basic java.

    I clearly explain with an example:
    You have an interval "A-H" which corresponds to ASCII code "65-72", you then transform into "0-7" subtracting both sides by the smaller (65 - 65; 72 - 65). Second, Java code:

    Code:
    java.util.Random random = new java.util.Random(7); 
    int res = random.nextInt();
    Finally, you no longer have to translate in the entire res character after having added 65 ...

    By imagining that res is equal to 5, you find yourself with 5 + 65 = 70 which corresponds to the letter F.

  4. #4
    Join Date
    Aug 2009
    Posts
    63

    Re: How to get a random value from a string

    It works by combining your 2 answers

    I actually thought there was a solution using the object directly but I found nothing in that sense.

    Anyway thank you ALL

  5. #5
    Join Date
    Nov 2008
    Posts
    996

    Re: How to get a random value from a string

    Assuming that the string makes always 3 characters with a hyphen as the second character, you can do:

    Code:
    static Random r = new Random();
     
    static char randomChar(String pattern) {
        int min = pattern.charAt(0), max = pattern.charAt(2);
        return (char) (min + r.nextInt(max-min+1));
    }

Similar Threads

  1. Replies: 4
    Last Post: 13-01-2012, 05:07 PM
  2. Convert string into int in C
    By screwball in forum Software Development
    Replies: 4
    Last Post: 22-12-2011, 08:47 PM
  3. How to use "Math.random()" to generate a random number in JavaScript?
    By Silent~Kid in forum Software Development
    Replies: 5
    Last Post: 03-02-2010, 05:06 AM
  4. How to Manipulate String using PHP String Functions
    By ComPaCt in forum Software Development
    Replies: 3
    Last Post: 21-09-2009, 09:07 AM
  5. SBS 2003 Rebooting at Random - Bugcheck String: 0x0000008e
    By Meng R in forum Small Business Server
    Replies: 3
    Last Post: 02-07-2005, 01:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,774,331.08380 seconds with 17 queries