我想从java.i中所需的概率数组字符串列表生成随机字符串,但不知道如何处理概率。我必须运行程序差不多25-30次从具有一定概率的字符串列表生成随机字符串
Probability for abc is 10%
for def is 60%
for ghi is 20%
for danny is 10%
但我无法做到这一点。
import java.util.*;
public class newyork
{
public static void main(String[]args) throws Exception
{
// othr fun
public static void abc()
{
//Strings to display
String [] random = {"abc","def", "ghi","danny"};
//Pick one by one
String no1= random[(int) (Math.random()*(random.length))];
String no2 = random[(int) (Math.random()*(random.length))];
String no3 = random[(int) (Math.random()*(random.length))];
//print randomly generated strings
System.out.println("Here you go : " + no1 + " " + no2 + " " + no3 + ");
}
所以,你想要随机字符串,基于'概率'。这意味着可能性越高,选择的时间越长。什么是随机的? –
你的代码在哪里使用了加权概率?为了它的工作,你应该尽量做*东西*与他们至少.. – Patrick
这是我不知道如何编写概率代码的主要事情.. – user1806895