当前位置:网站首页>Select randomly by weight [prefix and + dichotomy + random target]
Select randomly by weight [prefix and + dichotomy + random target]
2022-06-25 12:40:00 【REN_ Linsen】
The prefix and + Two points + Random target
Preface
Where there is order, there is dichotomy , In particular, the array elements are all larger than 0 Prefix and array , Two points is a sharp weapon .
One 、 Choose randomly by weight

Two 、 The prefix and + Two points + Random target
package everyday.medium;
import java.util.Arrays;
import java.util.Random;
// Choose randomly by weight
public class PickIndex {
/* Give an array of weights w, Select randomly by weight . Direct prefix and + Random integers + Two points search == return idx, Just get the subscript . */
public PickIndex(int[] w) {
prefix = new int[w.length];
// Assign values to prefixes and .
int sum = 0, idx = 0;
for (int i : w) prefix[idx++] = (sum += i);
System.out.println(Arrays.toString(prefix));
}
int[] prefix;
Random rand = new Random(1);
public int pickIndex() {
// Random integer
// bug1: If each time is random feed Are all the same , It produces the same number .
// Random rand = new Random(System.currentTimeMillis());
// rand.nextInt(n) Range [0,n)
int target = rand.nextInt(prefix[prefix.length - 1]);
// Binary search position in prefix and .
return binarySearch(target + 1);
}
// Find a comparison target A place just big or equal to .
private int binarySearch(int target) {
int low = 0, high = prefix.length - 1;
while (low < high) {
int mid = low + (high - low >>> 1);
int midVal = prefix[mid];
if (midVal < target) low = mid + 1;
else if (midVal > target) high = mid;// You can't subtract one , Because I went to the right , I can't go back . Coordinate to remove the integer to approximate .
else return mid;
}
return high;
}
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
Random rand = new Random(1);
System.out.println(rand.nextInt(100));
}
}
}
summary
1) Ordered array / Positive prefix and And The close connection of dichotomy .
reference
边栏推荐
- Flutter common commands and problems
- Introduction to jiuhongtianxia system development function -- jiuhongtianxia app development source code sharing
- The first techo day Tencent technology open day in 2022 will be held online on June 28
- Lighten the source code -- lighten the app system development function introduction to the beautiful world lighten the app system development source code in China
- laravel 9
- 冷启动的最优解决方案
- Renrenyue -- renrenyue system development source code sharing
- thinkphp3.2.5 GIF. class. php for php7.4
- Figure explanation of fiborache sequence
- Hook technology
猜你喜欢

An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down

冷启动的最优解决方案
![[on]learning dynamic and hierarchical traffic spatiotemporal features with transformer](/img/58/d68112a3d019de66150e2f5102f436.png)
[on]learning dynamic and hierarchical traffic spatiotemporal features with transformer

Laravel excel export

ECSHOP video list_ ECSHOP uploading video, video classification, video list playing video function

一篇文章讲清楚MySQL的聚簇/联合/覆盖索引、回表、索引下推

High performance + million level Excel data import and export

Service charge and time setting code sharing involved in crmeb withdrawal process

三入职场!你可以从我身上学到这些(附毕业Vlog)

Today, I will explain to you what is DFI and its development prospects
随机推荐
Thinkphp3 count ` *'problem
Mysql database logs binlog save aging (expire\u logs\u days)
Penetration tool environment - installing sqli labs in centos7 environment
ECSHOP product attribute color specification size stock item No. automatic combination
High imitation blue playing network disk file sharing to make money network disk PHP system source code
High performance + million level Excel data import and export
PHP replaces the key of a two-dimensional array with a specified element value
A commonly used statistical modeling method -- difference analysis
The network traceroute command is used to determine the path through which IP packets access the destination address.
Digital currency exchange -- digital currency exchange system development source code sharing
The R language cartools package divides data, the scale function scales data, and the NaiveBayes function of e1071 package constructs a naive Bayesian model
Introduction to jiuhongtianxia system development function -- jiuhongtianxia app development source code sharing
Yunfan mall -- Yunfan mall system development source code sharing
Arm V7 coprocessor
Disconnected: No supported authentication methods available (server sent: )
How can we differ LEFT OUTER JOIN vs Left Join [duplicate]
Slice and slice methods of arrays in JS
The first techo day Tencent technology open day in 2022 will be held online on June 28
2022 meisai topic C idea sharing + translation
ECSHOP commodity page multi-attribute batch purchase plug-ins ECSHOP wholesale plug-ins multi-attribute order placing, multi-attribute batch purchase of commodities