This article is a mirror article of machine translation, please click here to jump to the original article.

View: 14665|Reply: 0

[Java Source Code] java uses Random to generate pseudo-random numbers and make verification codes

[Copy link]
Posted on 11/7/2014 11:06:49 AM | | | |


The random algorithm implemented in the Random class is pseudo-random, that is, regular randomness. When performing randomness, the origin number of the random algorithm is called the seed number, and a certain transformation is made on the basis of the seed number to produce the required random number.


  1. import java.util.Random;
  2. import java.util.Scanner;


  3. public class yzm {

  4.         /**
  5.          * @param args
  6.          */
  7.         public static void main(String[] args) {
  8.                 // TODO Auto-generated method stub
  9.                 char[] arr={
  10.                                 '0','1','2','3','4','5','6','7','8','9',
  11.                                 'a','b','c','d','e','f','g','h','i','j',
  12.                                 'k','l','m','n','o','p','q','r','s','t',
  13.                                 'v','w','x','y','z'
  14.                 };
  15.                 Random rd=new Random();
  16.                
  17.                 String str="";
  18.                 for(int i=0;i<4;i++){
  19.                         int n=rd.nextInt(35);
  20.                         str=str+arr[n];
  21.                 }
  22.                 System.out.println(str);
  23.                 System.out.println("请输入验证码:");
  24.                 Scanner sn=new Scanner(System.in);
  25.                 String sin=sn.next();
  26.                 if(str.toLowerCase().equals(sin.toLowerCase())){
  27.                         System.out.println("验证码正确!");
  28.                 }else{
  29.                         System.out.println("验证码错误!");
  30.                 }
  31.                 //IT论坛 www.itsvse.com
  32.                
  33.         }

  34. }
Copy code






Previous:Thousands of practical QQ emoticons_The most spoofed Golden Panster emoticons in history
Next:C# calls RunDll32.exe clean up browser cache and junk
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com