简体   繁体   中英

Generate random codes in Client-Side of GWT

I have created a java gwt application in which I want to verify user's email address from client side, is there any way to generate random 5 character code on client side?

Any sort of help will be appreciated.

Something like this?

StringBuilder sb = new StringBuilder();
Random random = new Random();

for (int i=0;i<5;i++) {
    sb.append('a'+random.nextInt(26));
}
String code = sb.toString();

Why don't you test with Java Math.random() .You can simply get by it.

Here is useful formula for generating random numbers

(int)(Math.random() * (max - min) + min)

So , you can generate 5 random numbers as like...

String randomCodes = String.valueOf((int) (Math.random() * (99999 - 1) + 1));
    while (randomCodes.length() < 5) {
            randomCodes = "0" + randomCodes;
        }

You can use the RandomStringUtils from the Apache Commons project,

RandomStringUtils.randomAlphabetic(5);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM