I have a list of N objects.
I would like to insert X dummy objects which are randomly placed between the real N objects, spaced between (0, N).
So I tried the following code.
int[] dummyIndexes = new int[X];
int randomStep = N/X * 2; // *2 because the Mean is N/X/2
Random random = new Random();
int randIdx = 0;
for (int i=0; i < X; i++)
{
randIdx += random.nextInt(randomStep);
dummyIndexes[i] = randIdx;
}
This works alright, although I'm not getting a good distribution all the way to the end of the domain N.
What's a better way to do this ?
This is will ensure you have one random value between each N/X
randIdx = N * i / X + random.nextInt(N / X) + 1;
This will do the trick (but note that it will newer place anything at N, the largest value will be N-1)
int[] dummyIndexes = new int[X];
int randomStep = N/X;
Random random = new Random();
int randIdx = 0;
for (int i=0; i < X; i++)
{
randIdx = randomStep * i + random.nextInt(randomStep);
dummyIndexes[i] = randIdx;
}
Related
I am looking for an example on resizing an 1D array using the Spline Functions with the Apache Commons - Math.
What I need is a method to expand and/or shrink the input array (double[]).
I could not find a good example trying to search online.
The trick here is that you need two arrays to create a spline but you only have one. Thus you need to fabricate an array. You can assume that the input array contains your y values and that the new fabricated array contains your x values so for any given x you have a corresponding y.
Disclaimer, I have not tested this code so make sure to adjust accordingly.
// To expand the array
public static double[] expand(double[] array, int newSize) {
final int length = array.length;
// let's calculate the new step size
double step = (double) length / (newSize + 1);
// fabricated array of x values
double[] x = new double[length];
for(int i = 0; i < length; ++i) {
x[i] = i;
}
// using Linear interpolator but it can be any other interpolator
LinearInterpolator li = new LinearInterpolator(); // or other interpolator
PolynomialSplineFunction psf = li.interpolate(x, array);
double[] expandedArray = new double[newSize];
double xi = x[0];
for (int i = 0; i < newSize - 1; ++i) {
expandedArray[i] = psf.value(xi);
xi += step;
}
expandedArray[newSize - 1] = array[length - 1];
return expandedArray;
}
To shrink the array you can either decimate the input array i.e. just create a new smaller array and just take the values based on the new step size or use an interpolator as before:
// To shrink the array
public static double[] shrink(double[] array, int newSize) {
final int length = array.length;
// let's calculate the new step size
double step = (double) length / (newSize - 1);
// fabricated array of x values
double[] x = new double[length];
for(int i = 0; i < length; ++i) {
x[i] = i;
}
// using Linear interpolator but it can be any other interpolator
LinearInterpolator li = new LinearInterpolator(); // or other interpolator
PolynomialSplineFunction psf = li.interpolate(x, array);
double[] expandedArray = new double[newSize];
double xi = x[0];
for (int i = 0; i < newSize - 1; ++i) {
expandedArray[i] = psf.value(xi);
xi += step;
}
expandedArray[newSize - 1] = array[length - 1];
return expandedArray;
}
I have little problem , im starting learn java .
I need to create 2dimensional array , and i need fill this array in 10% only int 1 of course my code need fill this array randomly .
Need some hints how to fill in 10% .
public static void main(String[] args) {
int maxX = 10;
int maxY = 10;
int[][] Arr = new int[maxX][maxY];
Random r = new Random();
// random ints
for (int x = 0; x < maxX; x++) {
for (int y = 0; y < maxY; y++) {
Arr[x][y] = r.nextInt(2);
}
}
// printing Arr
for (int i = 0; i < Arr.length; i++) {
for (int j = 0; j < Arr[i].length; j++) {
System.out.print(Arr[i][j] + " ");
}
System.out.println();
}
}
Make the array, take a random row and column, while the percentage is not exceeded, check if the position has 0, if yes fill it with 1.
int[][] array = new int[N][N];
int percentage = N*N/10;
int filled = 0;
while(filled <= percentage)
{
Random rand = new Random();
int i = rand.nextInt(N+1);
int j = rand.nextInt(N+1);
if(array[i][j] == 0)
{
filled++;
array[i][j] = 1;
}
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
System.out.print(array[i][j] + " ");
}
System.out.println();
}
You can take the following steps:
Suppose you need to fill an N * N array.
Create a List and add to it (N * N) / 10 1s and (N * N * 9) / 10 0s. list.addAll(Collections.nCopies(count,1 or 0)) can help you here.
Run Collections.shuffle on that List to obtain random order.
Iterate over the elements of the List. The first N elements will become the first row the the 2D array, the next N elements will become the second row of the array, and so on...
An alternative to shuffling is to pick 10% x N random positions and put a 1 (if a 0 was in the position, otherwise pick another position). In pseudo code:
int[][] array = new int[N][N]
apply N * N / 10 times {
index = random(0 .. N * N)
if array(index) = 0 then array(index) = 1
else retry with another index
}
You will need to convert the index from 0 to N*N into a pair of i,j in the 2D array.
I would use "double random = Math.random();"
And then an if to check if the variable random is less or equal to 0.1
I am trying to generate 100 random numbers between 0 and 9, and display the count for each number
Is this solution correct? I want to improve, what could I have done better?
java.util.Scanner k = new Scanner(System.in);
int[] numbers = new int[100];
for (int i = 0;i<numbers.length;i++)
{
numbers[i] = (int) (Math.random() * 10);
}
int [] counts = new int[10];
for (int i = 0;i<numbers.length;i++)
{
counts[numbers[i]%numbers.length]++;
}
for (int i=0;i<counts.length;i++)
{
if (counts[i]>1)
System.out.println(i+1+" Generates: "+(counts[i])+" times");
else
System.out.println(i+1+" Generated: "+(counts[i])+" time");
}
I think you have overthought this, you only need one array - of 10 values (for the range 0 - 9 there are ten unique values), generate one hundred values in that range, and increment the value in the array (that is your count of numbers), then loop to display. Something like,
int[] numbers = new int[10];
for (int i = 0; i < 100; i++) {
int ndx = (int) (Math.random() * numbers.length);
numbers[ndx]++;
}
for (int i = 0; i < numbers.length; i++) {
System.out.printf("%d was generated %d times.%n", i, numbers[i]);
}
And in Java 8+ you might write the above with IntStream and lambda(s) like
int[] numbers = new int[10];
IntStream.range(0, 100).forEach( //
x -> numbers[(int) (Math.random() * numbers.length)]++);
IntStream.range(0, numbers.length) //
.forEachOrdered(x -> System.out.printf( //
"%d was generated %d times.%n", x, numbers[x]));
This is classwork. I've been reading and searching and everything is telling me to use the java.util.Random of which I understand how that works and wish I could use it. but my assignment specifically tells me to use the (int) (Math.random * number) of which I am having difficulty seeing where to apply into my array. everything I've seen has been the Random pulled from java.
It is the generate 100 random integers 0-9 and how many times they occur. If someone can assist?
My error is - Exception in "main" java.lang.array index out of bounds exemption:10 and obviously there is something in my code wrong too.
public class NumberOfTimes{
public static void main(String[] args){
int rand = (int)(Math.random() * 10);
int[] counts = new int [10];
for(int i = 0; i < 100; i++){
counts[i]++;
}//end for
System.out.println("number\t" + "occurence ");
for (int num = 0; num < counts.length; num++){
System.out.println(num + "\t" + counts[num]);
}//end for
}//end main
}//end NumberOfTimes
make this change
int[] counts = new int[100];
for (int i = 0; i < counts.length; i++) {
counts[i] = (int) (Math.random() * 10);
}// end for
Your array can only hold 10 items, and on your loop you are accessing more than 10. Can be solved in two ways.
Either Increase your array length
int[] counts = new int [100];
Or either decrease your count in for loop.
for(int i = 0; i < 10; i++){
I'm trying to use SplineInterpolator
and PolynomialSplineFunction to double a dataset. I think I'm fairly far along the path (I'm probably missing some exception handling):
SplineInterpolator splineInterp;
public double[] doubledArray(double[] y){
double[] yy = new double[y.length*2];
// make a double version of y w/ -1 for "null" values
for(int i = 0; i < yy.length; i++){
if(i%2 == 0)
yy[i] = y[i];
else if(i == yy.length-1)
yy[i] = yy[0];
else
yy[i] = -1;
}
// make a corresponding x array to satisfy SplineInterpolator.interpolate
double[] x = new double[y.length];
for(int i = 0; i < x.length; i++)
x[i] = i;
splineInterp = new SplineInterpolator();
PolynomialSplineFunction polySplineF = splineInterp.interpolate(x, y);
for(int i = 0; i < yy.length; i++){
if(yy[i] == -1){
yy[i] = polySplineF.value(i);
// breaks down halfway through polySplineF.value expects and array of y.length
}
}
return yy;
}
But the above is gonna crash in the last for loop at the latest. So, do I have the first part more or less right? After I have my polynomial spline function, how do I use that to create a larger dataset?
In case anybody is following along at home, here is the implementation I came up with for this:
private double[] multiplyArray(double[] y){
// An array 2 or 4 or N times bigger than the original:
double[] yy = new double[y.length*arrayMultiplier];
// An array representing the indices of the original:
double[] x = new double[y.length];
for(int i = 0; i < x.length; i++)
x[i] = i;
// Get and instance of SplineInterpolator:
SplineInterpolator splineInterp = new SplineInterpolator();
// Use that instance's interpolate() function to a PolynomialSplineFunction
// fitting your data, points y at indices x.
PolynomialSplineFunction polySplineF = splineInterp.interpolate(x, y);
// Use the PolynomialSplineFunction to fill in your larger array by supplying
// index values divided by the arrayMultiplier
for(int i = 0; i < yy.length; i++){
yy[i] = polySplineF.value((double)(i/arrayMultiplier));
}
return yy;
}
I also figured out how to do the probably more useful fill-in-blanks use if anybody needs it.