Java 8 Random - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Random class.

This code example shows how to use the following methods:
 
import java.util.Random; 
 
public class LongFieldDataBenchmark { 
 
    private static final Random RANDOM = new Random();
    private static final int SECONDS_PER_YEAR = 60 * 60 * 24 * 365;
 
    public static enum Data { 
        SINGLE_VALUES_DENSE_ENUM {
            public int numValues() { 
                return 1; 
            } 
            @Override 
            public long nextValue() { 
                return RANDOM.nextInt(16); 
            } 
        }, 
        SINGLE_VALUED_DENSE_DATE {
            public int numValues() { 
10
CodeRank
This code example shows how to use the following methods:
        ConfigFactory.load("remotecreation")); 
    final ActorRef actor = system.actorOf(Props.create(CreationActor.class),
        "creationActor"); 
 
    System.out.println("Started CreationSystem");
    final Random r = new Random();
    system.scheduler().schedule(Duration.create(1, SECONDS),
        Duration.create(1, SECONDS), new Runnable() {
          @Override 
          public void run() { 
            if (r.nextInt(100) % 2 == 0) {
              actor.tell(new Op.Multiply(r.nextInt(100), r.nextInt(100)), null);
            } else { 
              actor.tell(new Op.Divide(r.nextInt(10000), r.nextInt(99) + 1),
                  null); 
            } 
          } 
        }, system.dispatcher());
  } 
} 
10
CodeRank
This code example shows how to use the following methods:nextLong
    public static ByteBuffer guidAsBytes()
    { 
        StringBuilder sbValueBeforeMD5 = new StringBuilder();
        long time = System.currentTimeMillis();
        long rand = 0;
        rand = myRand.nextLong();
        sbValueBeforeMD5.append(s_id)
                        .append(":")
                        .append(Long.toString(time))
                        .append(":")
                        .append(Long.toString(rand));
 
        String valueBeforeMD5 = sbValueBeforeMD5.toString();
        return ByteBuffer.wrap(FBUtilities.threadLocalMD5Digest().digest(valueBeforeMD5.getBytes()));
    } 
 
    /* 
        * Convert to the standard format for GUID 
        * Example: C2FEEEAC-CFCD-11D1-8B05-00600806D9B6 
    */ 
9
CodeRank
This code example shows how to use the following methods:nextInt
        mPlayer.setDataSource("test:invoke_mock_media_player.so?url=ping");
 
        Parcel request = mPlayer.newRequest();
        Parcel reply = Parcel.obtain();
 
        int val = rnd.nextInt();
        request.writeInt(val);
        mPlayer.invoke(request, reply);
        assertEquals(val, reply.readInt());
   } 
} 
9
CodeRank
This code example shows how to use the following methods:nextFloat
			return -1; 
		} 
	} 
 
	public static final float random(final float pMin, final float pMax) {
		return pMin + RANDOM.nextFloat() * (pMax - pMin);
	} 
 
	/** 
	 * @param pMin inclusive! 
	 * @param pMax inclusive! 
	 * @return 
	 */ 
	public static final int random(final int pMin, final int pMax) {
		return pMin + RANDOM.nextInt(pMax - pMin + 1);
	} 
 
	public static final boolean isPowerOfTwo(final int n) {
		return ((n != 0) && (n & (n - 1)) == 0);
	} 
9
CodeRank
This code example shows how to use the following methods:nextDouble
  protected long getDatumAt(int index) {
    return values[index];
  } 
 
  public long randomValue() { 
    return valueAt(random.nextDouble());
  } 
 
  public abstract long valueAt(double probability);
} 
9
CodeRank
This code example shows how to use the following methods:nextGaussian
   * 
   * @return random long 
   */ 
  public static long delay(final double delay, final double range)
  { 
    return (long) Math.abs((RANDOM.nextGaussian() * range) + delay);
  } 
} 
9
CodeRank
This code example shows how to use the following methods:nextBytes
    private final static Random rand = new Random();
 
 
    private static byte[] randomBytes() { 
        final byte[] bytes = new byte[48];
        rand.nextBytes(bytes);
        return bytes;
    } 
 
 
    public static XATransactionId createXID(final int formatId, String uniqueQualifier) {
        final byte[] branchQualifier = randomBytes();
        final byte[] globalTransactionId = randomBytes();
        final XATransactionId xid =
                new XATransactionId(formatId, branchQualifier, globalTransactionId, uniqueQualifier);
        return xid;
    } 
} 
8
CodeRank
This code example shows how to use the following methods:
 * @author Matthias Broecheler (me@matthiasb.com) 
 */ 
 
public class ByteBufferUtilTest { 
 
    private static final Random random = new Random();
 
 
    @Test 
    public void testCompareRandom() { 
        int trials = 10000;
        for (int t = 0; t<trials; t++) {
            long val1 = Math.abs(random.nextLong());
            long val2 = Math.abs(random.nextLong());
            ByteBuffer b1 = ByteBufferUtil.getLongByteBuffer(val1);
            ByteBuffer b2 = ByteBufferUtil.getLongByteBuffer(val2);
            assertEquals(val1+" : " + val2,Longs.compare(val1,val2),ByteBufferUtil.compare(b1,b2));
            assertEquals(Longs.compare(val2,val1),ByteBufferUtil.compare(b2,b1));
            assertEquals(Longs.compare(val1,val1),ByteBufferUtil.compare(b1,b1));
        } 
10
CodeRank
This code example shows how to use the following methods:nextFloat, nextInt
	 * @return a LoanQuote for the given request 
	 */ 
	public LoanQuote quote(LoanRequest loanRequest) {
		Calendar calendar = Calendar.getInstance();
		LoanQuote loanQuote = new LoanQuote();
		Random random = new Random();
		loanQuote.setQuoteDate(calendar.getTime());
		calendar.add(Calendar.DAY_OF_YEAR, random.nextInt(25));
		loanQuote.setExpirationDate(calendar.getTime());
		loanQuote.setRate(random.nextFloat() + this.baseRate);
		loanQuote.setTerm(10 + random.nextInt(10));
		loanQuote.setAmount(250000 + random.nextInt(40000));
		loanQuote.setLender(this.name);
		return loanQuote;
	} 
 
} 
8
CodeRank

Related Java 8 Random Questions & Answers:

Other Java 8 Random Examples

15:	  private ObjectAnimator animation2;
16:	  private Button button;
17:	  private Random randon;
18:	  private int width;
19:	  private int height;
-
26:	    width = getWindowManager().getDefaultDisplay().getWidth();
27:	    height = getWindowManager().getDefaultDisplay().getHeight();
28:	    randon = new Random();
29:	
30:	    set = createAnimation();

Read More

12:				// shuffle is on - play a random song
13:				Random rand = new Random();
14:				currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
15:				playSong(currentSongIndex);
16:			} else{
-
12:				// shuffle is on - play a random song
13:				Random rand = new Random();
14:				currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
15:				playSong(currentSongIndex);
16:			} else{

Read More

24:		private static final int MAX_ATTEMPTS = 5;
25:	    private static final int BACKOFF_MILLI_SECONDS = 2000;
26:	    private static final Random random = new Random();
27:	
28:	    /**
-
24:		private static final int MAX_ATTEMPTS = 5;
25:	    private static final int BACKOFF_MILLI_SECONDS = 2000;
26:	    private static final Random random = new Random();
27:	
28:	    /**

Read More

338:	
339:		}
340:	
341:		/**
342:		 * When user starts moving the progress handler
-
338:	
339:		}
340:	
341:		/**
342:		 * When user starts moving the progress handler

Read More

See Code Examples for Java 8 Random Methods: