Jump to content


Photo

Mutation Of A String


  • Please log in to reply
2 replies to this topic

#1 alvin567

alvin567

    Advanced Member

  • Members
  • PipPipPip
  • 163 posts

Posted 25 December 2012 - 09:57 PM

//There will be 0.0 to 1.0% chance that the String will become "CHICKEN NUGGESTs"
public static String main(String arg){
Random rand = new Random();
int mutatePercentage = rand.nextInt(100);// if it's 0, means 0% chance. if it's 99, it means 1% chance
if(mutatePercentage==0){
return arg; // no change since % is 0
}
else if (rand.nextInt(10000) < mutatePercentage){
return mutateString(arg);
}

}


Hi there,
I found this piece of code from else where.

Where can I get the mutateString(arg) function?
Just because nobody complains doesn't mean all parachutes are perfect. - Benny Hill

#2 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 25 December 2012 - 10:01 PM

Probably the same place.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#3 lemmin

lemmin

    Advanced Member

  • Members
  • PipPipPip
  • 1,906 posts

Posted 26 December 2012 - 08:02 PM

My best guess: http://forums.hardwa...ge-4026916.html

public static String mutateString(String mutate){
   int max = 122;//largest ascii
   int min = 0;//smallest ascii range
   StringBuilder str = new StringBuilder(mutate);
   Random r = new Random();
   for(int i = 0; i < mutate.length();i++){
      if(0.50 >= r.nextDouble()){
         str.setCharAt(i, (char) r.nextInt(max - min + 1));
      }
   }
return str.toString();
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com