按一定的概率隨機出隨機數(小白記錄)

2021-10-13 12:46:18 字數 918 閱讀 5376

按一定的概率隨機出隨機數

例如:專案場景:專案需要每天凌晨0點準時發布,農場操作任務:如澆水,施肥,除草,除蟲等,需要按一定的比例隨機出每天的任務操作

public

class

randomtest

if(random > a && random <= a+b)

if(random > a+b && random <= a+b+c)

if(random > a+b+c)

else

}public

static

void

main

(string[

] args)

if(random==2)

if(random==3)

if(random==4)

} system.out.

println

("1 出現:"

+a+" 概率:"

+a/(a+b+c+d)

*100);

system.out.

println

("2 出現:"

+b+" 概率:"

+b/(a+b+c+d)

*100);

system.out.

println

("3 出現:"

+c+" 概率:"

+c/(a+b+c+d)

*100);

system.out.

println

("4 出現:"

+d+" 概率:"

+d/(a+b+c+d)

*100);

system.out.

println

("生成次數:"

+(a+b+c+d));

}}

生成一定範圍的隨機數

include using namespace std const float maxweight 0.3 const float scaleweight 32767 void main 5.產生一定範圍隨機數的通用表示公式 要取得 a,b 的隨機整數,使用 rand b a a 要取得 a,b 的...

按概率生成隨機數Java版本

思想什麼的可以參看http blog.csdn.net slowgrace archive 2009 03 25 4022632.aspx 如果要控制1個數落在某個區間的概率,比如要求在sngbegin和sngend之間生成乙個隨機數,這個隨機數落在sngpb和sngpe之間的概率是p 有兩種方法,...

按概率生成隨機數Java版本

思想什麼的可以參看 如果要控制1個數落在某個區間的概率,比如要求在sngbegin和sngend之間生成乙個隨機數,這個隨機數落在sngpb和sngpe之間的概率是p 有兩種方法,以第二種方法為好。先說第一種方法,要點是 1 由於sngpb和sngpe將整個區間分成3部分,所以先分別計算隨機數落在3...