Search Java Code Snippets


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





#Java - Code Snippets for '#Random' - 8 code snippet(s) found

 Sample 1. 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 2. Generate Random Number till 100

Random rand = new Random();
int randomNumber = rand.nextInt(100);

   Like      Feedback     Random Number Generation  Random


 Sample 3. 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 4. Code Sample / Example / Snippet of java.util.Random

  public Enumerable<Object[]> scan(DataContext root) {

final Random random = seed >= 0 ? new Random(seed) : new Random();

final Maze maze = new Maze(width, height);

final PrintWriter pw = new PrintWriter(System.out);

maze.layout(random, pw);

if (Maze.DEBUG) {

maze.print(pw, true);

}

return new AbstractEnumerable<Object[]>() {

public Enumerator<Object[]> enumerator() {

final Set<Integer> solutionSet;

if (solution) {

solutionSet = maze.solve(0, 0);

} else {

solutionSet = null;

}

return Linq4j.transform(maze.enumerator(solutionSet),

new Function1<String, Object[]>() {

public Object[] apply(String s) {

return new Object[] {s};

}

});

}

};

}


   Like      Feedback      java.util.Random


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 5. Code Sample / Example / Snippet of java.util.Random

    private File createRandomFileWithContent() throws IOException {

OutputStream fileOut = null;

File file = null;

try {

file = FileUtils.createTempFile(null);

fileOut = new FileOutputStream(file);

byte[] byteArray = new byte[12345];

Random randomContentCreator = new Random();

randomContentCreator.nextBytes(byteArray);

fileOut.write(byteArray);



return file;

}

finally {

try {

if (fileOut != null) {

fileOut.close();

}

}

catch (IOException e) {

throw e;

}

}

}


   Like      Feedback      java.util.Random


 Sample 6. Code Sample / Example / Snippet of org.apache.commons.crypto.random.CryptoRandom

    protected CryptoRandom getRandom(String className) throws Exception {

Properties props = new Properties();

props.setProperty(CryptoRandomFactory.CLASSES_KEY, className);

final CryptoRandom cryptoRandom = CryptoRandomFactory.getCryptoRandom(props);

Assert.assertEquals(className, cryptoRandom.getClass().getCanonicalName());

return cryptoRandom;

}


   Like      Feedback      org.apache.commons.crypto.random.CryptoRandom


 Sample 7. Sample Log4j config with RollingRandomAccessFile appender and TimeBasedTriggeringPolicy

<Configuration status="INFO">
<Appenders>
<RollingRandomAccessFile name="File" fileName="logs/xyz.log" immediateFlush="true" filePattern="logs/xyz-%d{MM-dd-yyyy}.log.gz">
      <TimeBasedTriggeringPolicy />
   </RollingRandomAccessFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="File" />
</Root>
</Loggers>
</Configuration>

   Like      Feedback     log4j  rolling logs with log4j  TimeBasedTriggeringPolicy  RollingRandomAccessFile


 Sample 8. Sample Log4j config file with RollingRandomAccessFile appender ( Rolling Logs ) and SizeBasedTriggeringPolicy

<Configuration status="INFO">
<Appenders>
<RollingRandomAccessFile name="File" fileName="logs/xyz.log" immediateFlush="true" filePattern="logs/xyz-%d{MM-dd-yyyy}.log.gz">
      <SizeBasedTriggeringPolicy size="250 MB"/>
   </RollingRandomAccessFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="File" />
</Root>
</Loggers>
</Configuration>

   Like      Feedback     log4j  SizeBasedTriggeringPolicy  RollingRandomAccessFile



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