I'm not sure at all how to go about this, but I want to generate 1000 randomly generated 7 digit numbers, and 5 letter names. The names could be like "FjwaS" anything, it doesnt have to be an actual name. I want to store all these values in 2 different arrays. telephone
array, then a name
array. I'm not sure how I should approach this at all.
我根本不确定如何解决这个问题,但我想生成1000个随机生成的7位数字和5个字母的名字。这些名字可能就像“FjwaS”一样,它不一定是真正的名字。我想将所有这些值存储在2个不同的数组中。电话阵列,然后是名字阵列。我不确定我该如何处理这个问题。
4 个解决方案
#1
1
Easy:
简单:
1 - create your collection
1 - 创建您的收藏
2 - iterate 1000 times
2 - 迭代1000次
..2a - generate random values
..2a - 生成随机值
..2b - populate your collection
..2b - 填充你的收藏
#2
1
Below is the code for a very basic implementation for the problem given above. Later, you can use your desired collection. Change the value of MAX
to your desired length. The main work is done by the randomInt()
and randomString()
functions.
下面是上面给出的问题的基本实现的代码。之后,您可以使用所需的集合。将MAX的值更改为所需的长度。主要工作由randomInt()和randomString()函数完成。
import java.util.*;
public class RandomDirectoryCreation
{
static final String alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main(String[] args)
{
final int MAX = 10;
String[] name = new String[MAX];
int[] telephone = new int[MAX];
for(int i=0; i<MAX; i++)
{
name[i] = randomString(5);
telephone[i] = randomInt(1000000, 9999999);
}
for(int i=0; i<MAX; i++)
{
System.out.println("Name: " + name[i] + " Telephone: " + telephone[i]);
}
}
public static int randomInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public static String randomString(int len)
{
Random rand = new Random();
StringBuilder word = new StringBuilder(len);
for( int i = 0; i < len; i++) {
word.append( alphabets.charAt(rand.nextInt(alphabets.length())));
}
return word.toString();
}
}
Sample Output:
样本输出:
Name: kBbSL Telephone: 4152479
Name: GOEat Telephone: 7473373
Name: KRBPq Telephone: 8346073
Name: yqjpu Telephone: 7553636
Name: yvRBA Telephone: 2133757
Name: ajUBg Telephone: 3826625
Name: BhBVr Telephone: 5714195
Name: AvNYB Telephone: 6179815
Name: DfsxM Telephone: 6611458
Name: gJRka Telephone: 2114751
References:
参考文献:
-
How do I generate random integers within a specific range in Java?
如何在Java中生成特定范围内的随机整数?
-
How to generate a random alpha-numeric string?
如何生成随机字母数字字符串?
#3
0
Why reinvent the wheel? You can use RandomStringUtils from Apache Commons.
为什么重新发明*?您可以使用Apache Commons的RandomStringUtils。
import org.apache.commons.lang3.RandomStringUtils;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[] names = new String[1000];
String[] telephones = new String[1000];
for (int i=0; i<1000; i++) {
String randomName = RandomStringUtils.randomAlphabetic(5);
names[i] = randomName;
String randomTelephone = RandomStringUtils.randomNumeric(7);
telephones[i] = randomTelephone;
}
System.out.println(Arrays.toString(names));
System.out.println(Arrays.toString(telephones));
}
}
#4
0
I had written it to generate a random string, you may change it according to your needs:
我写它来生成一个随机字符串,你可以根据你的需要改变它:
import java.util.Random;
public class randomWord
{
public static void main(String args[])
{
Random charp = new Random();
String[] chars = {"a", "b", "c", "d", "e", "f", "g", "h" ,"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "M", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
String[] word = new String[9];
for(int i = 0; i < 9;i++)
{
word[i] = chars[charp.nextInt(70)];
}
System.out.print("Your randomly generated string is: ");
for(int i = 0; i < 9;i++)
{
System.out.print(word[i]);
}
Random number = new Random();
System.out.println("\nRandom number: "+number.nextInt(6));
}
}
#1
1
Easy:
简单:
1 - create your collection
1 - 创建您的收藏
2 - iterate 1000 times
2 - 迭代1000次
..2a - generate random values
..2a - 生成随机值
..2b - populate your collection
..2b - 填充你的收藏
#2
1
Below is the code for a very basic implementation for the problem given above. Later, you can use your desired collection. Change the value of MAX
to your desired length. The main work is done by the randomInt()
and randomString()
functions.
下面是上面给出的问题的基本实现的代码。之后,您可以使用所需的集合。将MAX的值更改为所需的长度。主要工作由randomInt()和randomString()函数完成。
import java.util.*;
public class RandomDirectoryCreation
{
static final String alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main(String[] args)
{
final int MAX = 10;
String[] name = new String[MAX];
int[] telephone = new int[MAX];
for(int i=0; i<MAX; i++)
{
name[i] = randomString(5);
telephone[i] = randomInt(1000000, 9999999);
}
for(int i=0; i<MAX; i++)
{
System.out.println("Name: " + name[i] + " Telephone: " + telephone[i]);
}
}
public static int randomInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public static String randomString(int len)
{
Random rand = new Random();
StringBuilder word = new StringBuilder(len);
for( int i = 0; i < len; i++) {
word.append( alphabets.charAt(rand.nextInt(alphabets.length())));
}
return word.toString();
}
}
Sample Output:
样本输出:
Name: kBbSL Telephone: 4152479
Name: GOEat Telephone: 7473373
Name: KRBPq Telephone: 8346073
Name: yqjpu Telephone: 7553636
Name: yvRBA Telephone: 2133757
Name: ajUBg Telephone: 3826625
Name: BhBVr Telephone: 5714195
Name: AvNYB Telephone: 6179815
Name: DfsxM Telephone: 6611458
Name: gJRka Telephone: 2114751
References:
参考文献:
-
How do I generate random integers within a specific range in Java?
如何在Java中生成特定范围内的随机整数?
-
How to generate a random alpha-numeric string?
如何生成随机字母数字字符串?
#3
0
Why reinvent the wheel? You can use RandomStringUtils from Apache Commons.
为什么重新发明*?您可以使用Apache Commons的RandomStringUtils。
import org.apache.commons.lang3.RandomStringUtils;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[] names = new String[1000];
String[] telephones = new String[1000];
for (int i=0; i<1000; i++) {
String randomName = RandomStringUtils.randomAlphabetic(5);
names[i] = randomName;
String randomTelephone = RandomStringUtils.randomNumeric(7);
telephones[i] = randomTelephone;
}
System.out.println(Arrays.toString(names));
System.out.println(Arrays.toString(telephones));
}
}
#4
0
I had written it to generate a random string, you may change it according to your needs:
我写它来生成一个随机字符串,你可以根据你的需要改变它:
import java.util.Random;
public class randomWord
{
public static void main(String args[])
{
Random charp = new Random();
String[] chars = {"a", "b", "c", "d", "e", "f", "g", "h" ,"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "M", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
String[] word = new String[9];
for(int i = 0; i < 9;i++)
{
word[i] = chars[charp.nextInt(70)];
}
System.out.print("Your randomly generated string is: ");
for(int i = 0; i < 9;i++)
{
System.out.print(word[i]);
}
Random number = new Random();
System.out.println("\nRandom number: "+number.nextInt(6));
}
}