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

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

本文整理了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

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

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

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

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

public static byte[] generateArray(int size) {
  byte[] retval=new byte[size];
  for(int i=0; i < retval.length; i++) {
    byte b=(byte)Util.random(26);
    retval[i]=b;
  }
  return retval;
}

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

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

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

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

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

/**
 * Reorders elements of an array in-place. No bounds checking is performed. Null elements are shuffled, too
 * @param array the array to be shuffled; the array will be modified
 * @param from the start index inclusive
 * @param to the end index (exclusive), must be >= from (not checked)
 * @param <T> the type of the array's elements
 */
public static <T> void shuffle(T[] array, int from, int to) {
  if(array == null)
    return;
  for(int i=from; i < to; i++) {
    int random=(int)random(to);
    int other=random -1 + from;
    // int other=(int)(random(to)-1 + from);
    if(i != other) {
      T tmp=array[i];
      array[i]=array[other];
      array[other]=tmp;
    }
  }
}

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

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

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

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

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

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

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

public static <T> T pickRandomElement(Set<T> set) {
  if(set == null || set.isEmpty()) return null;
  int size=set.size();
  int random=(int)Util.random(size)-1;
  for(Iterator<T> it=set.iterator(); it.hasNext();) {
    T el=it.next();
    if(random-- <= 0)
      return el;
  }
  return null;
}

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

public static String generateLocalName() {
  String retval=null;
  try {
    retval=shortName(InetAddress.getLocalHost().getHostName());
  }
  catch(Throwable ignored) {
  }
  if(retval == null) {
    try {
      retval=shortName(InetAddress.getByName(null).getHostName());
    }
    catch(Throwable e) {
      retval="localhost";
    }
  }
  long counter=Util.random((long)Short.MAX_VALUE * 2);
  return retval + "-" + counter;
}

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

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

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

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

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

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

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

/**
 * Returns a random value within [min_interval - max_interval]
 */
long computeInterval() {
  return min_interval + Util.random(max_interval - min_interval);
}

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

public static byte[] generateArray(int size) {
  byte[] retval=new byte[size];
  for(int i=0; i < retval.length; i++) {
    byte b=(byte)Util.random(26);
    retval[i]=b;
  }
  return retval;
}

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

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

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

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

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

/**
 * Returns a random value within [min_interval - max_interval]
 */
long computeInterval() {
  return min_interval + Util.random(max_interval - min_interval);
}

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

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

相关文章

Util类方法