当前位置:网站首页>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
边栏推荐
- ECSHOP commodity wholesale multi attribute multi specification multi inventory batch purchase ECSHOP wholesale plug-in ECSHOP multi attribute order
- Figure explanation of fiborache sequence
- JS array de duplication
- Possible causes of wechat applet decryption failure
- Mysql database logs binlog save aging (expire\u logs\u days)
- Zhangxiaobai's road to penetration (7) -sql injection detailed operation steps -union joint query injection
- (6) Pyqt5--- > window jump (registration login function)
- Lighten the source code -- lighten the app system development function introduction to the beautiful world lighten the app system development source code in China
- Jeecgboot startup popup configuration is still incorrect
- Thinkphp3 count ` *'problem
猜你喜欢

laravel 9
![最大数[抽象排序之抽象规则]](/img/47/f6bafacc95f487854a3e304325f3f0.png)
最大数[抽象排序之抽象规则]

GNSS receiver technology and application review

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

A commonly used statistical modeling method -- difference analysis

K8s, docker compose install MySQL 8.0.18

Installation and removal of MySQL under Windows

Shell learning notes (latest update: 2022-02-18)
![[data midrange] what is the oneid of the data midrange? Isn't the master data fragrant?](/img/51/9aceaaeed1f6db7c60685d53477eba.png)
[data midrange] what is the oneid of the data midrange? Isn't the master data fragrant?

Execution order of MySQL query statements join, on and where
随机推荐
Thinkphp3 use phpword to modify the template and download it
Idea2017 how to set not to automatically open a project at startup
flutter 收到推送后自动消失问题
The R language uses the follow up The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The type parameter specifies the line type of
Go novice exploration pause
Upgrade opsenssh to 8.8p1
Concat(), join(), reverse(), sort() method in JS array
Zhangxiaobai's road of penetration (VI) -- the idea and process of SQL injection and the concat series functions and information of SQL_ Schema database explanation
ECSHOP upload video_ ECSHOP video list, video classification, video related product guide
Go from 0 to 1. Obtain the installation package, get, post request, params, body and other parameters
Penetration tool environment - Installation of sqlmap
Full nanny tutorial of Market Research Competition (experience sharing)
Possible causes of wechat applet decryption failure
Zunpin Yongyao advertising e-commerce system -- Zunpin Yongyao advertising e-commerce app system development source code sharing
What is the primordial universe
Ten commandments of self-learning in machine learning
Arm V7 LDR STR memory access
Heyangdao store management system -- share the development source code of heyangdao system
ECSHOP quickly purchases goods, simplifies the shopping process, and improves the user experience through one-step shopping
The source code of the hottest online disk money making system in 2022 imitates Lanzou online disk / Chengtong online disk / sharing money making cloud disk system / online disk VIP Download System