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]; }
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; }
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; }
long computeSleepTime() { return Util.random((desired_avg_gossip * 2)); } }
/** * 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; }
/** * 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; } } }
@Override public long nextInterval() { if(++num_writes > max_writes) return 0; // discontinues this task return Math.max(1000, Util.random(sleep_interval)); }
public long nextInterval() { return Math.max(min_interval, Util.random(max_interval) + max_interval/2); }
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); }
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; }
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; }
/** 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)); } }
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); }
/** 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; }
/** * Returns a random value within [min_interval - max_interval] */ long computeInterval() { return min_interval + Util.random(max_interval - min_interval); }
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; }
/** * 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; }
long computeSleepTime() { return Util.random((desired_avg_gossip * 2)); } }
/** * Returns a random value within [min_interval - max_interval] */ long computeInterval() { return min_interval + Util.random(max_interval - min_interval); }
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]; }