TechArena Community How to get a random value from a string

#1
22-10-2009
 Member 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
22-10-2009
 Member 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]{
}else{
}
return vector.get(myRandom(vector.size()));
}```
#3
22-10-2009
 Member 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
22-10-2009
 Member Join Date: Aug 2009 Posts: 63
Re: How to get a random value from a string

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

Anyway thank you ALL
#5
22-10-2009
 Member 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));
}```

 Tags:

 Similar Threads for: "How to get a random value from a string" Thread Thread Starter Forum Replies Last Post Generate some quality of random number via Random Number Generation function aMADeO! Windows Software 4 13-01-2012 05:07 PM Convert string into int in C screwball Software Development 4 22-12-2011 08:47 PM How to use "Math.random()" to generate a random number in JavaScript? Silent~Kid Software Development 5 03-02-2010 05:06 AM How to Manipulate String using PHP String Functions ComPaCt Software Development 3 21-09-2009 09:07 AM SBS 2003 Rebooting at Random - Bugcheck String: 0x0000008e Meng R Small Business Server 3 02-07-2005 01:44 AM

All times are GMT +5.5. The time now is 11:00 PM.