J
John
Hi All, I have created the following class to uniformly generate
numbers according to a zipf distribution. How can I extend this program
so that I can generate two data points with the requirement that the
first data point is correlated with the first? ie. the secondpoint will
vary with the first? any ideas? thanks in advance.
import java.util.Random;
/*
Basically a simple program to generate random
variables which approximate a Zipf distribution
*/
public class basic_zipf
{
/* Calculate the generalized harmonic number of order N.
Basically, the sum of the reciprocals. */
public final static void harmonic(double exponent, double[]
sum_reciprocals)
{
double sum = 0.0;
for (int i = 1; i <= sum_reciprocals.length; i++)
{
sum += 1.0 / Math.pow(i,exponent);
sum_reciprocals[i-1]=sum;
}
}
public static void main(String[] args)
{
int numer_of_files=1000;
/* random number generator */
Random random=new Random();
double exponent=1.0;
/* get the harmonic number of order 30 */
//double sum = harmonic(numer_of_files,exponent);
/* lookup table for each reciprocal value 1 ... 30 */
double reciprocals[]=new double[numer_of_files];
/* calculate harmonic numbers */
harmonic(exponent,reciprocals);
double sum=reciprocals[numer_of_files-1];
int[] variates=new int[numer_of_files];
/* generate 10,000 random variables */
for(int i=0;i<500000;i++)
{
// scale the random variable to the range [0,sum]
double tmp=random.nextDouble()*sum;
double range=0;
// bin the scaled random number and output the bin number
for(int j=0;j<numer_of_files;j++)
{
// find out which range the value is in
double upper=reciprocals[j];
if((tmp>=range) && (tmp<=upper))
{
//System.out.println(j+1);
variates[j]++;
break;
}
else
{
range=upper;
}
}
}
for(int i=0;i<variates.length;i++)
{
System.out.println((i+1)+","+variates);
}
}
}
numbers according to a zipf distribution. How can I extend this program
so that I can generate two data points with the requirement that the
first data point is correlated with the first? ie. the secondpoint will
vary with the first? any ideas? thanks in advance.
import java.util.Random;
/*
Basically a simple program to generate random
variables which approximate a Zipf distribution
*/
public class basic_zipf
{
/* Calculate the generalized harmonic number of order N.
Basically, the sum of the reciprocals. */
public final static void harmonic(double exponent, double[]
sum_reciprocals)
{
double sum = 0.0;
for (int i = 1; i <= sum_reciprocals.length; i++)
{
sum += 1.0 / Math.pow(i,exponent);
sum_reciprocals[i-1]=sum;
}
}
public static void main(String[] args)
{
int numer_of_files=1000;
/* random number generator */
Random random=new Random();
double exponent=1.0;
/* get the harmonic number of order 30 */
//double sum = harmonic(numer_of_files,exponent);
/* lookup table for each reciprocal value 1 ... 30 */
double reciprocals[]=new double[numer_of_files];
/* calculate harmonic numbers */
harmonic(exponent,reciprocals);
double sum=reciprocals[numer_of_files-1];
int[] variates=new int[numer_of_files];
/* generate 10,000 random variables */
for(int i=0;i<500000;i++)
{
// scale the random variable to the range [0,sum]
double tmp=random.nextDouble()*sum;
double range=0;
// bin the scaled random number and output the bin number
for(int j=0;j<numer_of_files;j++)
{
// find out which range the value is in
double upper=reciprocals[j];
if((tmp>=range) && (tmp<=upper))
{
//System.out.println(j+1);
variates[j]++;
break;
}
else
{
range=upper;
}
}
}
for(int i=0;i<variates.length;i++)
{
System.out.println((i+1)+","+variates);
}
}
}