Jump to content


Mutation Of A String

  • Please log in to reply
2 replies to this topic

#1 alvin567

  • Members
  • PipPipPip
  • Advanced Member
  • 163 posts

Posted 26 December 2012 - 02:57 AM

//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
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

  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 26 December 2012 - 03:01 AM

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

  • Members
  • PipPipPip
  • Advanced Member
  • 1,906 posts

Posted 27 December 2012 - 01:02 AM

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