mardi 23 décembre 2014

ArrayIndexOutOfBoundsException error in darts file

The purpose of this program is to find an estimate of pi. At first it ran but the values were too large (20,30,,40,etc) and now it does not even compile. I get the ArrayIndexOutOfBoundsException error. How would you go on fixing the code? Thanks.



import java.util.Scanner;
import java.lang.Math;
import java.util.Random;
import java.io.File;
import java.io.IOException;
public class Darts
{
public static double[] getPi(double[] pi, int trials, int times, int counter)
{
for (int loop = 0; loop < trials; loop++)
{
for (int r = 0; r < times; r++)
{
double x = Math.random();
double y = Math.random();


if (Math.pow(x,2) + Math.pow(y,2) <= 1)
{
counter++;
}


}
pi[trials] = 4 * (double) counter / times;

}
return pi;
}
public static void printResults(double[] pi, int trials, double average)
{
for (int x = 0; x < trials; x++)
{
System.out.println("Trial [ " + x + "]: pi = " + pi[x]);
}
System.out.println("Estimate of pi = " + average );
}

public static void main (String [ ] args) throws IOException
{
Scanner in = new Scanner(System.in);
int counter = 0;
double y = 0;
double x = 0;
double radius = 1.0;
Random randNumList = new Random();


System.out.println("How many times darts should be thrown in a trial?");
int times = in.nextInt();

System.out.print("How many trials will there be?");
int trials = in.nextInt();
System.out.println(" " + trials + " trials");

double pi[] = new double[trials];
pi = getPi(pi, times, trials, counter);

double sumPi = 0.0;
for (int l = 0; l < trials; l++)
{
sumPi += pi[l];
}
double average = sumPi/counter;
printResults(pi, trials, average);


}
}




Randomly break up array into chunks of at least 3 with even distribution

I have an array of size n, and would like to break it up into m chunks of size at least 3. For example, given the array



[1,2,3,4,5,6,7,8,9,10]


and m=3, we could break the it up into



a=[1,2,3,4][5,6,7][8,9,10]
b=[1,2,3][4,5,6,7][8,9,10]
c=[1,2,3][4,5,6][7,8,9,10]


We could think of these solutions as being represented by the pairs (4,3,3) (3,4,3) and (3,3,4). I would like a function that given an array, n, and m, returns a random solution AND returns these solutions with an even distribution (so that you are no more likely to get one particular solution than any other). (This function needs to work for n=50, so for performance reasons we cannot do this by calculating all possible solutions.)





Why does this random value has 25/75 distribution instead of 50/50?

Edit: so basically what I'm trying to write is a 1 bit hash for double.


I want to map a double to true or false with 50/50 chance. For that I wrote code that picks some random numbers (just as an example, I want to use this on data with regularities and still get 50/50 result), checks their last bit and increments y if it is 1, or n if it is 0. However, this code constantly results in 25% y and 75% n. Why is it not 50/50? And why such a weird, but straight-forward (1/3) distribution?



public class DoubleToBoolean {
@Test
public void test() {

int y = 0;
int n = 0;
Random r = new Random();
for (int i = 0; i < 1000000; i++) {
double randomValue = r.nextDouble();
long lastBit = Double.doubleToLongBits(randomValue) & 1;
if (lastBit == 1) {
y++;
} else {
n++;
}
}
System.out.println(y + " " + n);
}
}


Example output:



250167 749833




SecureRandom with NativePRNG vs SHA1PRNG

I need to generate cryptographically strong random numbers and byte arrays. For this purpose, I'm using Java's SecureRandom class. But I'm not sure to choose which PRNG algorithm in terms of their cryptographic strength.


Which of the following instances generates a more unpredictable numbers? Or are they equal?



SecureRandom nativePrng = SecureRandom.getInstance("NativePRNG")
SecureRandom sha1Prng = SecureRandom.getInstance("SHA1PRNG")


Moreover, we are able to generate these instances with "SUN" provider (e.g. SecureRandom.getInstance("SHA1PRNG", "SUN")). Do this make a difference?


Thanks in advance.





lundi 22 décembre 2014

Generate random integer: Algorithm

I know most programming languages include some sort of method to generate random integers within a specified domain, but I'm particularly interested in how those methods are built. I'm wondering if there is some sort of standardised algorithm for generating random integers given a domain, like there is for Linear Search, Binary Search, Bubble Sort etc. Thank you





How to add two random numbers within a range?

My requirement :--



The sum of the two random numbers will be 100..
sum=random_no1 + random_no2 (the sum will be exact 100)


So,I have tried:--



Random r = new Random();
int Low = 10;
int High = 100;
int R = r.nextInt(High-Low) + Low;

Random r1 = new Random();
int Low1 = 10;
int High1 = 100;
int R1 = r1.nextInt(High1-Low1) + Low1;


but how I define the sum??


I can not understand..Please help me..





Exponentially Distributed Random Variates with mean 500 in C

I want to create bunch of random numbers but it should be exponentially distributed. I should use Steve Park & Dave Geyer's Random Number Generation basically I need to add a function to create this. So I did this but it seems it stuck in while. How can I fix this? Thanks in advance!



double RandomRV(void) //Exponential Distribution


{



double i;
double numb;

i = Random();

numb = i / MODULUS;

while (numb < 1.e-6)
{
i = Random();
numb = i / MODULUS;
}

return((double)-500*log(numb));


}