Share

Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Math.random()' - 4 code snippet(s) found

 Sample 1. Generate a Random float number from 0 to 1

double randomNo = Math.random();
System.out.println(randomNo);

   Like      Feedback     random number   random number generation  Math.random()


 Sample 2. Generate a Random Integer from 0 to 100

double randomNo = Math.random();
System.out.println((int)(randomNo*100));

   Like      Feedback     random number   random number generation  Math.random()


 Sample 3. Code Sample / Example / Snippet of org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream

    public void readOfLength0ShouldReturn0() throws Exception {

final byte[] rawData = new byte[1048576];

for (int i=0; i < rawData.length; ++i) {

rawData[i] = (byte) Math.floor(Math.random()*256);

}



final ByteArrayOutputStream baos = new ByteArrayOutputStream();

final BZip2CompressorOutputStream bzipOut = new BZip2CompressorOutputStream(baos);

bzipOut.write(rawData);

bzipOut.flush();

bzipOut.close();

baos.flush();

baos.close();



final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

final BZip2CompressorInputStream bzipIn = new BZip2CompressorInputStream(bais);

final byte[] buffer = new byte[1024];

Assert.assertEquals(1024, bzipIn.read(buffer, 0, 1024));

Assert.assertEquals(0, bzipIn.read(buffer, 1024, 0));

Assert.assertEquals(1024, bzipIn.read(buffer, 0, 1024));

bzipIn.close();

}


   Like      Feedback      org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream


 Sample 4. Code Sample / Example / Snippet of org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream

    public void readOfLength0ShouldReturn0() throws Exception {

final byte[] rawData = new byte[1048576];

for (int i=0; i < rawData.length; ++i) {

rawData[i] = (byte) Math.floor(Math.random()*256);

}



final ByteArrayOutputStream baos = new ByteArrayOutputStream();

final BZip2CompressorOutputStream bzipOut = new BZip2CompressorOutputStream(baos);

bzipOut.write(rawData);

bzipOut.flush();

bzipOut.close();

baos.flush();

baos.close();



final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

final BZip2CompressorInputStream bzipIn = new BZip2CompressorInputStream(bais);

final byte[] buffer = new byte[1024];

Assert.assertEquals(1024, bzipIn.read(buffer, 0, 1024));

Assert.assertEquals(0, bzipIn.read(buffer, 1024, 0));

Assert.assertEquals(1024, bzipIn.read(buffer, 0, 1024));

bzipIn.close();

}


   Like      Feedback      org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus