// JAVA code
public static void main( String[] args ) throws Exception
{
for(int num: new int[]{10,100,1000, 10000,100000,1000000})
probByNum( num );
}
private static void probByNum( int num )
{
//final char[] chars = { 'а', 'б', 'в' };
final double[] probs = { 0.12d, 0.5d, 1.0d }; // cumulative probability for each value
int[] res = new int[ 3 ];
Random rnd = new Random( ( new Date() ).getTime() ); // each time different seed
for ( int i = 1; i <= num; i++ )
{
double prob = rnd.nextDouble();
for ( int j = 0; j < 3; j++ )
{
if ( prob < probs[ j ] )
{
res[ j ]++; // count detected probability
break;
}
}
}
System.out.println(
String.format( Locale.ENGLISH,"Probability with %10d trials to hit 0.120, 0.380, 0.500 is: %4.3f, %4.3f, %4.3f",
num,
( ( ( double ) res[ 0 ] ) / ( double ) num ),
( ( ( double ) res[ 1 ] ) / ( double ) num ),
( ( ( double ) res[ 2 ] ) / ( double ) num ) ) );
}
// Results:
Probability with 10 trials to hit 0.120, 0.380, 0.500 is: 0.100, 0.200, 0.700
Probability with 100 trials to hit 0.120, 0.380, 0.500 is: 0.080, 0.410, 0.510
Probability with 1000 trials to hit 0.120, 0.380, 0.500 is: 0.122, 0.375, 0.503
Probability with 10000 trials to hit 0.120, 0.380, 0.500 is: 0.124, 0.377, 0.499
Probability with 100000 trials to hit 0.120, 0.380, 0.500 is: 0.122, 0.377, 0.501
Probability with 1000000 trials to hit 0.120, 0.380, 0.500 is: 0.119, 0.380, 0.501
if ( !file.getParentFile().exists())
{
java.nio.file.Files.createDirectory( file.getParentFile().toPath() );
}
/**
* Tries to remove all files (and sub-directories optionally) in the designated
* directory
*
* @param dir_path directory path to clear from other files and optionally
* directories
* @param recursive deletes sub-dirs also if set to {@code true}
* @return {@code true} if operation completed successfully that is no errors
* occurs during operation, else {@code false}
*/
public static boolean clearDirectory( String dir_path, boolean recursive )
{
File dir = new File( dir_path );
if ( !dir.isDirectory() )
{
return false;
}
boolean res = true;
String[] files = dir.list();
String fdir = Files.resetDir( dir_path );
String fpath;
StringBuffer sb = new StringBuffer();
for ( int i = 0; i < files.length; i++ )
{
sb.delete( 0, Integer.MAX_VALUE ).append( fdir ).append( files[ i ] );
File file = new File( fpath = sb.toString() );
if ( file.isDirectory() && recursive )
{
res = clearDirectory( fpath, recursive );
}
else
{
try
{
res = file.delete(); // TODO: здесь забирать конкретное имя фала в общий выходной массив
}
catch ( SecurityException se )
{
}
}
}
return res;
}
/**
* The system-dependent default name-separator character. This field is
* initialised to contain the first character of the value of the system
* property file.separator. On UNIX systems the value of this field is '/'; on
* Microsoft Windows systems it is '\\'.
*/
public static final char FileSeparator = File.separatorChar;
/**
* The system-dependent default name-separator character. This field is
* initialised to contain the first character of the value of the system
* property file.separator. On UNIX systems the value of this field is '/'; on
* Microsoft Windows systems it is '\\'.
*/
public static final char FS = File.separatorChar;
/**
* appends system dependent file separator char
*
* @param FilePath - string to check for file separator existence
* @return new string with appended file separator or the same string if it
* already was present
*/
public static String appendFileSeparator( String FilePath )
{
return appendFileSeparator( FilePath, FileSeparator );
}
/**
* appends system dependent file separator char
*
* @param FilePath - string to check for file separator existence
* @param sep character to be a last separator in the input string
* @return new string with appended file separator or the same string if it
* already was present
*/
public static String appendFileSeparator( String FilePath, char sep )
{
if ( Text.isEmpty( FilePath ) )
{
return "" + sep;
}
char ch = FilePath.charAt( FilePath.length() - 1 );// get last symbol
if ( ( ch != '/' ) && ( ch != '\\' ) )// check it to be a separator one
{
return FilePath + sep;
}
else
{
return FilePath.substring( 0, FilePath.length() - 1 ) + sep;
}
}
/**
* the same as {@link #appendFileSeparator} but with shorter name :o)
*
* @param path path to append file separator
* @return path with trailing current file names separator
*/
public static String resetDir( String path )
{
if ( path == null )
{
return null;
}
return appendFileSeparator( path );
}