随机数生成总是 returns 相同的值
Random number generation always returns same value
我正在尝试 运行 一个简单的程序,其中服务器侦听连接、接收客户端请求并发送消息。也许微不足道,但我的困惑是关于服务器的行为。代码如下:
服务器代码:
import java.net.*;
import java.io.*;
import java.util.*;
public class haikuServer {
private static String chooseHaiku() {
String[] haiku = new String[3];
haiku[0] = "Pawprints disappear"+"\n"+"into the snowy glen, but"+"\n"+"fox waits patiently.";
haiku[1] = "Whispering winds cry"+"\n"+"while frenzied snowflakes scatter,"+"\n"+"searching for others.";
haiku[2] = "The path, hard and long"+"\n"+"brings dawn with passage of time"+"\n"+"and then my heart sings.";
Random t = new Random();
int ch = t.nextInt(3);
return haiku[ch];
}
public static void main(String[] args) {
try {
ServerSocket sock = new ServerSocket(5575);
while (true) {
Socket client = sock.accept();
PrintWriter pout = new PrintWriter(client.getOutputStream(), true);
String haiku = chooseHaiku();
pout.println(haiku);
client.close();
}
} catch (Exception e) {}
}
}
客户代码:
import java.net.*;
import java.io.*;
public class haikuClient {
public static void main(String[] args) {
try {
Socket sock = new Socket("127.0.0.1", 5575);
InputStream in = sock.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
System.out.println("\nHaiku:\n");
String line;
while ((line = bin.readLine()) != null) {
System.out.println(line);
}
System.out.println();
/* Close the socket connection */
sock.close();
} catch (IOException ioe) {
System.err.println(ioe);
}
}
}
代码运行良好。我编译了这两个文件。对于 运行 它们,我使用:java haikuServer &(& 运行 后台程序)然后是 haikuClient。我的问题是我尝试 运行 haikuClient 多次,但一直以来,服务器选择的是第一个,即 haiku[0] 而不是其他。为什么会这样?
与网络无关,与随机数生成有关。
您观察到仅返回数组中的第一项是不正确的...当我 运行 您的代码时,它会在数组的前两个元素之间交替。
nextInt
的文档指出:
Returns a pseudorandom, uniformly distributed int value between 0
(inclusive) and the specified value (exclusive)
所以这个调用:nextInt(2)
returns 一个介于 0 和 1 之间的数字。如果您 运行 客户端足够多次,您将看到返回的俳句在第一个和第二个之间交替阵列。要获得您想使用的三个随机元素中的任何一个 nextInt(3)
.
我正在尝试 运行 一个简单的程序,其中服务器侦听连接、接收客户端请求并发送消息。也许微不足道,但我的困惑是关于服务器的行为。代码如下:
服务器代码:
import java.net.*;
import java.io.*;
import java.util.*;
public class haikuServer {
private static String chooseHaiku() {
String[] haiku = new String[3];
haiku[0] = "Pawprints disappear"+"\n"+"into the snowy glen, but"+"\n"+"fox waits patiently.";
haiku[1] = "Whispering winds cry"+"\n"+"while frenzied snowflakes scatter,"+"\n"+"searching for others.";
haiku[2] = "The path, hard and long"+"\n"+"brings dawn with passage of time"+"\n"+"and then my heart sings.";
Random t = new Random();
int ch = t.nextInt(3);
return haiku[ch];
}
public static void main(String[] args) {
try {
ServerSocket sock = new ServerSocket(5575);
while (true) {
Socket client = sock.accept();
PrintWriter pout = new PrintWriter(client.getOutputStream(), true);
String haiku = chooseHaiku();
pout.println(haiku);
client.close();
}
} catch (Exception e) {}
}
}
客户代码:
import java.net.*;
import java.io.*;
public class haikuClient {
public static void main(String[] args) {
try {
Socket sock = new Socket("127.0.0.1", 5575);
InputStream in = sock.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
System.out.println("\nHaiku:\n");
String line;
while ((line = bin.readLine()) != null) {
System.out.println(line);
}
System.out.println();
/* Close the socket connection */
sock.close();
} catch (IOException ioe) {
System.err.println(ioe);
}
}
}
代码运行良好。我编译了这两个文件。对于 运行 它们,我使用:java haikuServer &(& 运行 后台程序)然后是 haikuClient。我的问题是我尝试 运行 haikuClient 多次,但一直以来,服务器选择的是第一个,即 haiku[0] 而不是其他。为什么会这样?
与网络无关,与随机数生成有关。
您观察到仅返回数组中的第一项是不正确的...当我 运行 您的代码时,它会在数组的前两个元素之间交替。
nextInt
的文档指出:
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)
所以这个调用:nextInt(2)
returns 一个介于 0 和 1 之间的数字。如果您 运行 客户端足够多次,您将看到返回的俳句在第一个和第二个之间交替阵列。要获得您想使用的三个随机元素中的任何一个 nextInt(3)
.