-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRandomString_24.java
More file actions
47 lines (35 loc) · 1.18 KB
/
RandomString_24.java
File metadata and controls
47 lines (35 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.Scanner;
//Java program generate a random AlphaNumeric String
//using Math.random() method
public class RandomString {
// function to generate a random string of length n
static String getAlphaNumericString(int n)
{
// chose a Character random from this String
String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvxyz";
// create StringBuffer size of AlphaNumericString
StringBuilder sb = new StringBuilder(n);
for (int i = 0; i < n; i++) {
// generate a random number between
// 0 to AlphaNumericString variable length
int index
= (int)(AlphaNumericString.length()
* Math.random());
// add Character one by one in end of sb
sb.append(AlphaNumericString
.charAt(index));
}
return sb.toString();
}
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
System.out.println("enter the size of random string ");
// Get the size n
int n = sc.nextInt();
// Get and display the alphanumeric string
System.out.println(RandomString
.getAlphaNumericString(n));
}
}