org.jgroups.util.Util.random()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(6.9k)|赞(0)|评价(0)|浏览(197)

本文整理了Java中org.jgroups.util.Util.random()方法的一些代码示例,展示了Util.random()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Util.random()方法的具体详情如下:
包路径:org.jgroups.util.Util
类名称:Util
方法名:random

Util.random介绍

[英]Returns a random value in the range [1 - range]. If range is 0, 1 will be returned. If range is negative, an exception will be thrown
[中]返回[1-range]范围内的随机值。如果范围为0,则返回1。如果范围为负,将引发异常

代码示例

代码示例来源:origin: wildfly/wildfly

  1. public static <T> T pickRandomElement(T[] array) {
  2. if(array == null) return null;
  3. int size=array.length;
  4. int index=(int)Util.random(size)-1;
  5. return array[index];
  6. }

代码示例来源:origin: wildfly/wildfly

  1. protected static byte[] generateRandomBytes(int size) {
  2. byte[] retval=new byte[size]; // here we'd have to generate a buffer with random contents
  3. for(int i=0; i < retval.length; i++)
  4. retval[i]=(byte)Util.random(Byte.MAX_VALUE);
  5. return retval;
  6. }

代码示例来源:origin: wildfly/wildfly

  1. public static byte[] generateArray(int size) {
  2. byte[] retval=new byte[size];
  3. for(int i=0; i < retval.length; i++) {
  4. byte b=(byte)Util.random(26);
  5. retval[i]=b;
  6. }
  7. return retval;
  8. }

代码示例来源:origin: wildfly/wildfly

  1. long computeSleepTime() {
  2. return Util.random((desired_avg_gossip * 2));
  3. }
  4. }

代码示例来源:origin: wildfly/wildfly

  1. /**
  2. * Tosses a coin weighted with probability and returns true or false. Example: if probability=0.8,
  3. * chances are that in 80% of all cases, true will be returned and false in 20%.
  4. */
  5. public static boolean tossWeightedCoin(double probability) {
  6. if(probability >= 1)
  7. return true;
  8. if(probability <= 0)
  9. return false;
  10. long r=random(100);
  11. long cutoff=(long)(probability * 100);
  12. return r < cutoff;
  13. }

代码示例来源:origin: wildfly/wildfly

  1. /**
  2. * Reorders elements of an array in-place. No bounds checking is performed. Null elements are shuffled, too
  3. * @param array the array to be shuffled; the array will be modified
  4. * @param from the start index inclusive
  5. * @param to the end index (exclusive), must be >= from (not checked)
  6. * @param <T> the type of the array's elements
  7. */
  8. public static <T> void shuffle(T[] array, int from, int to) {
  9. if(array == null)
  10. return;
  11. for(int i=from; i < to; i++) {
  12. int random=(int)random(to);
  13. int other=random -1 + from;
  14. // int other=(int)(random(to)-1 + from);
  15. if(i != other) {
  16. T tmp=array[i];
  17. array[i]=array[other];
  18. array[other]=tmp;
  19. }
  20. }
  21. }

代码示例来源:origin: wildfly/wildfly

  1. @Override
  2. public long nextInterval() {
  3. if(++num_writes > max_writes)
  4. return 0; // discontinues this task
  5. return Math.max(1000, Util.random(sleep_interval));
  6. }

代码示例来源:origin: wildfly/wildfly

  1. public long nextInterval() {
  2. return Math.max(min_interval, Util.random(max_interval) + max_interval/2);
  3. }

代码示例来源:origin: wildfly/wildfly

  1. public static <T> T pickRandomElement(List<T> list) {
  2. if(list == null || list.isEmpty()) return null;
  3. int size=list.size();
  4. int index=(int)Util.random(size)-1;
  5. return list.get(index);
  6. }

代码示例来源:origin: wildfly/wildfly

  1. public static <T> T pickRandomElement(Set<T> set) {
  2. if(set == null || set.isEmpty()) return null;
  3. int size=set.size();
  4. int random=(int)Util.random(size)-1;
  5. for(Iterator<T> it=set.iterator(); it.hasNext();) {
  6. T el=it.next();
  7. if(random-- <= 0)
  8. return el;
  9. }
  10. return null;
  11. }

代码示例来源:origin: wildfly/wildfly

  1. public static String generateLocalName() {
  2. String retval=null;
  3. try {
  4. retval=shortName(InetAddress.getLocalHost().getHostName());
  5. }
  6. catch(Throwable ignored) {
  7. }
  8. if(retval == null) {
  9. try {
  10. retval=shortName(InetAddress.getByName(null).getHostName());
  11. }
  12. catch(Throwable e) {
  13. retval="localhost";
  14. }
  15. }
  16. long counter=Util.random((long)Short.MAX_VALUE * 2);
  17. return retval + "-" + counter;
  18. }

代码示例来源:origin: wildfly/wildfly

  1. /**
  2. Schedules a stability message to be mcast after a random number of milliseconds (range [1-stability_delay] secs).
  3. The reason for waiting a random amount of time is that, in the worst case, all members receive a
  4. STABLE_GOSSIP message from the last outstanding member at the same time and would therefore mcast the
  5. STABILITY message at the same time too. To avoid this, each member waits random N msecs. If, before N
  6. elapses, some other member sent the STABILITY message, we just cancel our own message. If, during
  7. waiting for N msecs to send STABILITY message S1, another STABILITY message S2 is to be sent, we just discard S2.
  8. @param tmp A copy of the stability digest, so we don't need to copy it again
  9. */
  10. protected void sendStabilityMessage(Digest tmp, final ViewId view_id) {
  11. if(send_stable_msgs_to_coord_only || stability_delay <= 1)
  12. _sendStabilityMessage(tmp, view_id);
  13. else {
  14. // give other members a chance to mcast STABILITY message. if we receive STABILITY by the end of our random
  15. // sleep, we will not send the STABILITY msg. this prevents that all mbrs mcast a STABILITY msg at the same time
  16. startStabilityTask(tmp, view_id, Util.random(stability_delay));
  17. }
  18. }

代码示例来源:origin: wildfly/wildfly

  1. protected void sendDiscoveryResponse(Address logical_addr, PhysicalAddress physical_addr,
  2. String logical_name, final Address sender, boolean coord) {
  3. final PingData data=new PingData(logical_addr, is_server, logical_name, physical_addr).coord(coord);
  4. final Message rsp_msg=new Message(sender).setFlag(Message.Flag.INTERNAL, Message.Flag.OOB, Message.Flag.DONT_BUNDLE)
  5. .putHeader(this.id, new PingHeader(PingHeader.GET_MBRS_RSP)).setBuffer(marshal(data));
  6. if(stagger_timeout > 0) {
  7. int view_size=view != null? view.size() : 10;
  8. int rank=Util.getRank(view, local_addr); // returns 0 if view or local_addr are null
  9. long sleep_time=rank == 0? Util.random(stagger_timeout)
  10. : stagger_timeout * rank / view_size - (stagger_timeout / view_size);
  11. timer.schedule(() -> {
  12. log.trace("%s: received GET_MBRS_REQ from %s, sending staggered response %s", local_addr, sender, data);
  13. down_prot.down(rsp_msg);
  14. }, sleep_time, TimeUnit.MILLISECONDS, sends_can_block);
  15. return;
  16. }
  17. log.trace("%s: received GET_MBRS_REQ from %s, sending response %s", local_addr, sender, data);
  18. down_prot.down(rsp_msg);
  19. }

代码示例来源:origin: org.jgroups/com.springsource.org.jgroups

  1. /**
  2. Tosses a coin weighted with probability and returns true or false. Example: if probability=0.8,
  3. chances are that in 80% of all cases, true will be returned and false in 20%.
  4. */
  5. public static boolean tossWeightedCoin(double probability) {
  6. long r=random(100);
  7. long cutoff=(long)(probability * 100);
  8. return r < cutoff;
  9. }

代码示例来源:origin: org.jgroups/com.springsource.org.jgroups

  1. /**
  2. * Returns a random value within [min_interval - max_interval]
  3. */
  4. long computeInterval() {
  5. return min_interval + Util.random(max_interval - min_interval);
  6. }

代码示例来源:origin: org.jboss.eap/wildfly-client-all

  1. public static byte[] generateArray(int size) {
  2. byte[] retval=new byte[size];
  3. for(int i=0; i < retval.length; i++) {
  4. byte b=(byte)Util.random(26);
  5. retval[i]=b;
  6. }
  7. return retval;
  8. }

代码示例来源:origin: org.jboss.eap/wildfly-client-all

  1. /**
  2. * Tosses a coin weighted with probability and returns true or false. Example: if probability=0.8,
  3. * chances are that in 80% of all cases, true will be returned and false in 20%.
  4. */
  5. public static boolean tossWeightedCoin(double probability) {
  6. if(probability >= 1)
  7. return true;
  8. if(probability <= 0)
  9. return false;
  10. long r=random(100);
  11. long cutoff=(long)(probability * 100);
  12. return r < cutoff;
  13. }

代码示例来源:origin: org.jboss.eap/wildfly-client-all

  1. long computeSleepTime() {
  2. return Util.random((desired_avg_gossip * 2));
  3. }
  4. }

代码示例来源:origin: org.jgroups/com.springsource.org.jgroups

  1. /**
  2. * Returns a random value within [min_interval - max_interval]
  3. */
  4. long computeInterval() {
  5. return min_interval + Util.random(max_interval - min_interval);
  6. }

代码示例来源:origin: org.jboss.eap/wildfly-client-all

  1. public static <T> T pickRandomElement(T[] array) {
  2. if(array == null) return null;
  3. int size=array.length;
  4. int index=(int)Util.random(size)-1;
  5. return array[index];
  6. }

相关文章

Util类方法