我想生成一个随机的唯一String并将其放入哈希表。我确实喜欢下面的代码,但它不是唯一的,而只是一个字符。
while(k<4){
for(int j=1 ; j<=13 ;j++){
Hashtable<Integer, String> Deck = new Hashtable();
int myChar = (int) (Math.random()*str.length());
StringBuilder sb = new StringBuilder();
sb.append(str.charAt(myChar));
int i = (int) (Math.random() *52) ;
Deck.put(i, sb.toString());
System.out.print(Deck);
}
System.out.println();
k++;
}
输出是这个
{30=d}{9=e}{11=b}{10=d}{43=g}....
我想成为
{30=dea}{9=egt}{11=brf}{10=dgrtg}{43=grrh}{14=gwrt}.....
答案 0 :(得分:0)
尝试使用以下代码通过随机方法生成唯一密钥。
WITH t AS (
SELECT t.*, v.[Interval],
AVG(v.[Interval]) OVER () as avg_interval
FROM MyTable t CROSS JOIN
(VALUES (DATEDIFF(minute, DateTimeCreated, DateTimeUpdated))
) v(Interval)
WHERE DateTimeCreated > CAST(CAST(GETDATE() AS date) AS datetime)
)
SELECT AVG(CASE WHEN t.[Interval] < t.avg_interval THEN t.[Interval] END) AS AverageEntryInterval,
AVG(CASE WHEN t.[Interval] > t.avg_interval THEN t.[Interval] END) AS AverageExitInterval
FROM t;
答案 1 :(得分:0)
您可以使用Apache Commons Lang生成字母字符串
String generatedString = RandomStringUtils.randomAlphanumeric(10);
尝试这样的事情
Hashtable<Integer, String> Deck = new Hashtable();
for (int i = 0; i < 10; i++) {
String generatedString = RandomStringUtils.randomAlphanumeric(10);
int key = (int) (Math.random() *52) ;
Deck.put(key, generatedString);
}
System.out.println(Deck);
OutPut
{9=Ut7N87oMNp, 8=7kbARh5WIy, 7=pbU2ZCOGK1, 6=vAGAIw41Us, 5=VLnpY1FAuN, 4=UEIJNIvZlt, 3=z6Y3zXcDY1, 2=PxaMqXl8XW, 1=l72bkPdY6T, 0=FFdOsKpQgd}
如果只希望使用字母,请使用RandomStringUtils.randomAlphabetic(10);
的{{1}}。
如果您不希望使用第三方api,则可以在代码段中进行一些更改
RandomStringUtils.randomAlphanumeric(10)
仅在while循环中添加了
答案 2 :(得分:0)
您可以使用以下代码生成随机字符串。
private static final String ALPHA_STRING = "abcdefghijklmnopqrstuvwxyz";
public static String randomString(int count) {
StringBuilder builder = new StringBuilder();
while (count-- != 0) {
int character = (int) (Math.random() * ALPHA_STRING.length());
builder.append(ALPHA_STRING.charAt(character));
}
return builder.toString();
}
public static void main(String[] arg) {
Hashtable<Integer, String> Deck = new Hashtable();
for (int j = 1; j <= 10; j++) {
Deck.put(j, randomString(12));
}
System.out.println(Deck);
}
输出
{10=upjolkfihcyh, 9=irpbrcoclcmx, 8=ppugclscjlja, 7=wtnvkpmocuwd, 6=gpyzppvywazp, 5=vosixtjvcqlg, 4=plwtzzjpzeoq, 3=wsdyyppphhng, 2=knqdzfctfnez, 1=jzakqfiksrho}