Last active
December 14, 2015 13:58
-
-
Save sys1yagi/5097530 to your computer and use it in GitHub Desktop.
generate seed for specify string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.dip.sys1.yagi.randomstring; | |
import java.util.Random; | |
public class GenSeed { | |
public static class Seed{ | |
long seed; | |
int length; | |
public Seed(long seed, int length){ | |
this.seed = seed; | |
this.length = length; | |
} | |
@Override | |
public String toString() { | |
Random random = new Random(seed); | |
StringBuilder sb = new StringBuilder(); | |
for(int i = 0; i < length; i++){ | |
int j = random.nextInt(27); | |
sb.append((char)('`'+j)); | |
} | |
return sb.toString(); | |
} | |
} | |
public static Seed findSeed(String want){ | |
if(want == null || want.length() == 0){ | |
return null; | |
} | |
int length = want.length(); | |
Random random = new Random(); | |
for(long i = Long.MIN_VALUE; i <= Long.MAX_VALUE; i++){ | |
random.setSeed(i); | |
StringBuilder sb = new StringBuilder(); | |
for(int j = 0; j < length; j++){ | |
int k = random.nextInt(27); | |
sb.append((char)('`' + k)); | |
} | |
if(sb.toString().equals(want)){ | |
return new Seed(Long.valueOf(i), length); | |
} | |
} | |
return null; | |
} | |
public static void main(String[] args) { | |
Seed helloSeed = findSeed("hello"); | |
Seed vvakameSeed = findSeed("vvakame"); | |
if(helloSeed != null && vvakameSeed != null){ | |
System.out.println(helloSeed.toString() + " " + vvakameSeed.toString()); | |
} | |
else{ | |
System.out.println("seed not found."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment