So I'm working on an OOP program that is meant to create 50 unique numbers, by using a random number generator that ensures no repeating numbers. I have the random part down and I'm using an extra method which swaps the numbers but I don't know to only swap number if they are already used, I hope this is understandable.
import java.util.Random;
public class Project_1
{
public static void main(String[] args)
{
Random rand = new Random();
int a[][] = new int[11][6];
for (int i = 1; i <= 10; i++) //row of values which fill in Student number
{
System.out.print("Student " + i + ":");
System.out.print("\t");
for (int j = 1; j <= 5; j++) //j is limited up to 5 columns
{
a[i][j] = 1 + rand.nextInt(50);
CheckNumbers(a[i][j]); //input of the checkNumbers method
System.out.print(a[i][j] + "\t"); // meaning that the numbers fill out according to table coordinates
}
System.out.println();
}
}
public static void CheckNumbers(int[] x, int[] y)
{
int temp;
for (int j = 0; j < 50; j++) //j must start at 1??? and we have 50 iterations, so countercontrolled to 50?
{
temp = x[i]; //this can be used to swap the numbers
x[i] = y[i];
y[i] = temp;
}
}
}
This is my program and if ran without the input of the CheckNumbers method my answer was
----jGRASP exec: java Project_1
Student 1: 8 35 8 5 40
Student 2: 46 3 44 40 8
Student 3: 27 47 28 11 3
Student 4: 30 25 43 8 34
Student 5: 3 12 45 6 5
Student 6: 19 37 33 14 14
Student 7: 9 31 6 39 32
Student 8: 16 6 23 28 31
Student 9: 19 34 49 42 11
Student 10: 26 3 17 16 15
----jGRASP: operation complete.
However as you see 8 is repeated (among may other numbers) so I put in the CheckNumbers method to stop it but I have an error filled compile message... so I am assuming that I have many errors in my CheckNumbers, so I need major help with that too.
Thank you all for your help!!!!
Xincrol algorithm solves this problem. Explanation + source code:
http://openpatent.blogspot.co.il/2013/04/xincrol-unique-and-random-number.html
Your method definition of CheckNumbers is incorrect.
It should receive int[][] and not two int[] int[].
However its fairly impossible for me to figure out what the intent of that method is.
Do you have to generate the IDs yourself using java.util.Random? I would use java.lang.UUID which will generate an immutable universally unique identifier.
I'm not going to correct your methods. But if you want to create a random without repeating, create your own custom class.
Usage:
EDIT: JavaDoc added!!
I use
Collections.shuffle(<List>)
to generate random numbers. It is very quick way to generate numbers with minimum lines of code.First create an Integer array and will it with number of integers that you want to randomize. In the below example I have considered 10. Then use
Collections.shuffle(<List>)
to randomize the Integer array. Then print out the array.Handy way to get non-repeating list of numbers in random order:
Create an ArrayList with ints from 1 - 50
Use Collections.Shuffle
(you also should fix your nested loop and allocations so your indices start at 0 consistently-- but to learn you should do some of this yourself)