Help with random numbers please
Hi, I'm currently makin a program for an independent study project for school and am stumped as to how to set up a certain part of the program. I'm setting up a match card game and I need it to randomly arrange the cards when it starts off. I'm trying to figure out the best way to get random numbers between a certain set of numbers and every time i call the method, it'll select a different random number without repeating any of the numbers previously picked from other times I called it.
That might be a little confusing so here's an example
Say I tell it to pick a number between 1-5.
It'll pick say.. 4 the first time... 1the next time.. etc and will never pick those numbers again so long as it is called in that instance.
[760 byte] By [
kRnpandaa] at [2007-10-2 20:14:36]

Here's an example:
import java.util.*;
public class RandomNumberBag {
private List<Integer> numbers;
public RandomNumberBag(int size) {
numbers = new ArrayList<Integer>();
for(int i = 1; i <= size; i++) {
numbers.add(new Integer(i));
}
Collections.shuffle(numbers);
}
public int getNextRandom() {
if(numbers.size() == 0)
throw new RuntimeException("Nothing more in this bag!");
return numbers.remove(numbers.size()-1).intValue();
}
}
You can modify it to your needs.
thanks for the help . But one problem that occurred when I compiled it was that it said
PingryMatchCard.java:18: <identifier> expected
private List<Integer> numbers;
^
PingryMatchCard.java:21: '(' or '[' expected
numbers = new ArrayList<Integer>();
(pingry matchard is my program)
Can anyone offer advice as to how to fix it? The code I inserted into the program is almost an exact duplicate of what prometheuzz wrote. I'm not familiar enough with List<E> to fix it.
> thanks for the help . But one problem that occurred
> when I compiled it was that it said
>
> PingryMatchCard.java:18: <identifier> expected
> private List<Integer> numbers;
That means you don't have JDK 1.5 installed.
This should work:
import java.util.*;
public class RandomNumberBag {
private List numbers;
public RandomNumberBag(int size) {
numbers = new ArrayList();
for(int i = 1; i <= size; i++) {
numbers.add(new Integer(i));
}
Collections.shuffle(numbers);
}
public boolean hasMore() {
return (numbers.size() >= 1);
}
public int getNextRandom() {
if(numbers.size() == 0)
throw new RuntimeException("Nothing more in this bag!");
return ((Integer)numbers.remove(numbers.size()-1)).intValue();
}
public static void main(String[] args) {
RandomNumberBag bag = new RandomNumberBag(10);
while(bag.hasMore()) {
System.out.print(" ("+bag.getNextRandom()+") ");
}
}
}
I also put a main-method in it for you to test it.
I am taking a class and just learning java. I have an assignment that I'm struggling with that instructs me to generate 2 random integers from -100 to 100 (inclusive) and then get the sum of the two integers. I have tried to understand to posting you gave in response to but it seems very complex for what I'm trying to do. Do you think you could help me?
Thanks so much,
Kal
Don't post new questions on old threads.
You can generate numbers in any range by using code like this:// Do this once
Random rnd = new java.util.Random();
// Use rnd to get random numbers in a given range (starting from zero):
int rn1 = rnd.nextInt(50); // number in range 0..49
int rn2 = rnd.nextInt(25); // number in range 0..24
// And you can modify the range like this:
int rn3 = rnd.nextInt(50) + 50; // number in range 50..99
int rn4 = rnd.nextInt(25) + 1; // number in range 1..25
int rn5 = rnd.nextInt(201) - 100; // number in range -100..100
Thank you for the help. Sorry about the posting on an old thread.... I'm pretty new to forums. Won't happen again. So, with what you told me, this is what I got:
import java.util.Random;
public class RanNum1
{
public static void main(String[] args)
{
Random rnd = new java.util.Random();
int num1 = rnd.nextInt(100) - 100;
System.out.printf("Random Integer 1: %d\n", num1);
Random RanNum2 = new Random();
int num2 = rnd.nextInt(100) - 100;
System.out.printf("Random Integer 2: %d\n", num2);
int sum;
sum = num1 + num2;
System.out.printf("Sum: %d\n", sum);
}
}
Now, everything is working the way it should except, I've played with the ranges and I can only get it to give me positives or negatives but not a combination.
Thanks again,
Kal
I gave you this example...
int rn5 = rnd.nextInt(201) - 100; // number in range -100..100
And you shouldn't make a new generator every time - just call new Random() once as I showed in my example. Actually I just noticed, that although you make a second one - "Random RanNum2 = new Random();" you never actually use it - so you can delete that line of code.
Message was edited by:
TimRyanNZ
Thanks for the help. I appreciate it.Kal