Reputation: 1
I'm working on a project right now and I'm in a bit of a sticky situation. I'm trying to generate eight consecutive random numbers but with a catch. The value between one and another must not exceed five and the number must not be repeated. For example:
5 5 3 12 would need to be: 5 (Something else) 3 (Something else)
I've been running back and forth with the Random() option but can't seem to get this to work. So I've decided to predetermine the Integers so that the Random would have to choose between them.
int One = 1, Two = 3, Three = 5, Four = 7, Five = 8;
Random RNumber = new Random();
int RInteger = RNumber.nextInt(One, Two, Three, Four, Five);
Yeah and as you probably know... It doesn't work. Now I'm only a beginner at Java so I'm asking you for some advice. Or to let me know wether or not I'm approaching this matter correctly. Thanks in advance and have a good day.
In case you're interested. The error:
No suitable method found for nextInt(int, int, int, int, int)
method Random.nextInt(int) is not applicable
Upvotes: 0
Views: 934
Reputation: 23
Seems to me it's just a matter of generating a random number between -5 and 5 and add it to the last number in the list. Just make sure the number is not 0 so the last number is not repeated.
Pseudocode:
Let n = random number
output n
repeat {
repeat {
Let r = random number between -5 and 5
} until (r != 0)
next_n = n + r
output next_n
n = next_n
} until (enough numbers are output)
Upvotes: 0
Reputation: 1
So hello everbody once again. I've been playing with your responses and found them very interesting and helpful. However, I did mention that I've just started programming so it was a bit difficult for me to understand how they work. In light of that I decided to play around and try to create my own version. This is what I've come up with.
import java.util.Random;
public class Main
{
public static void main(String[] args)
{
int RInteger, Loop1 = 0, Loop2 = 0, Loop3 = 0;
Random RNumber = new Random();
while (Loop1 == 0) //Generate the first number
{
RInteger = RNumber.nextInt(8);
if (RInteger == 1)
{
Loop1 = 1;
}
if (RInteger == 5)
{
Loop1 = 5;
}
if (RInteger == 7)
{
Loop1 = 7;
}
}
while (Loop2 == 0) //Generates the second number making sure no duplicates are present and the maximum difference of 5
{
RInteger = RNumber.nextInt(8);
if (RInteger == 1 && (RInteger != Loop1) && (RInteger - Loop1) <= 5 && (RInteger - Loop1) >= -5)
{
Loop2 = 1;
}
if (RInteger == 5 && (RInteger != Loop1) && (RInteger - Loop1) <= 5 && (RInteger - Loop1) >= -5)
{
Loop2 = 5;
}
if (RInteger == 7 && (RInteger != Loop1) && (RInteger - Loop1) <= 5 && (RInteger - Loop1) >= -5)
{
Loop2 = 7;
}
}
while (Loop3 == 0) //A repeat of Loop2 with changed Loop1s into Loop2s and Loop2s into Loop3s
{
RInteger = RNumber.nextInt(8);
if (RInteger == 1 && (RInteger != Loop2) && (RInteger - Loop2) <= 5 && (RInteger - Loop2) >= -5)
{
Loop3 = 1;
}
if (RInteger == 5 && (RInteger != Loop2) && (RInteger - Loop2) <= 5 && (RInteger - Loop2) >= -5)
{
Loop3 = 5;
}
if (RInteger == 7 && (RInteger != Loop2) && (RInteger - Loop2) <= 5 && (RInteger - Loop2) >= -5)
{
Loop3 = 7;
}
}
System.out.println(Loop1 + " " + Loop2 + " " + Loop3);
}
}
It is fully functional, but very inefficient. If more numbers should be added the corresponding ifs should also be implemented respectively. Also another loop for each extra number.
I hope this helps somebody. Again, thanks for all your replies.
Have a great day.
Upvotes: 0
Reputation: 4228
If you want to get random ints from a defined list of integers then put the Integers
inside a List
and then shuffle it with Collections.shuffle
method. Get the first item of the list and then, delete from the list the last number you take and repeat the process.
A sample code that uses what I said and checks the numbers are maximun difference is 5:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class RandomNumbers {
public static int OFFSET = 5;
public static void main(String[] s){
RandomNumbers rn = new RandomNumbers();
int prev = 20; //Set prev randomly or any way you want to initialize the list
for (int i = 0; i < 10; i++){
prev = rn.getRandomWithinOffset(prev);
System.out.println(prev);
}
}
private int getRandomWithinOffset (int prev){
List<Integer> listNumbers = new ArrayList<Integer>();
for (int i = prev -OFFSET; i < prev + OFFSET; i++){
listNumbers.add(i);
}
listNumbers.remove(new Integer(prev));
Collections.shuffle(listNumbers);
return listNumbers.get(0);
}
}
Upvotes: 2
Reputation: 533500
I would use a loop like
public static List<Integer> generate(int count, int maxDiff, int min, int max) {
Set<Integer> ret = new LinkedHashSet<Integer>();
Random rand = new Random();
int last = rand.nextInt(max - min + 1) + min;
ret.add(last);
while(ret.size() < count) {
int next = rand.nextInt(max - min + 1) + min;
if (Math.abs(next - last) <= maxDiff) {
ret.add(next); // will ignore duplicates
last = next;
}
}
return new ArrayList<Integer>(ret);
}
Upvotes: 5
Reputation: 9775
Make your own function:
public generatePseudoRandom(int currentInt) {
Random random= new Random();
Integer result= null;
while (result == null || result.intValue() < currentInt - 5 || result.intValue() > currentInt + 5 || result.intValue() == currentInt) {
result = random.nextInt(current + 5);
}
return result;
}
It will generate new numbers until its within your border of five.
Upvotes: 1
Reputation: 9813
NextInt takes in the maximum number you need. For example numbers 0-20. nextInt(20) (this will only give a random from 0-20. You can't do more than one number.
To do what you need, make an array of your integers (5,10,20,etc)... Then do nextInt(array.size()). This will give you an index to a random number. Then get the number from that index.
Arraylist<Integer> randomNumbers = new Arraylist<Integer>();
randomNumbers.add(5);
randomNumbers.add(10);
randomNumbers.add(15);
Random gen = new Random();
int index = gen.nextInt(randomNumbers.size());
int randomInt = randomNumbers.get(index);
Upvotes: 0