It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Random ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random ArrayList" Watch "Random ArrayList" New topic
Author

Random ArrayList

James Eman
Ranch Hand

Joined: Mar 10, 2013
Posts: 45
I'm not sure if this is a simple question or not it sounds easy. i need a method that will give me a random number that the range will be constantly changing. i have 3 different lets call them levels there is probably a better name for them but they act like a levels so just say they are. there are 3 levels each with a different number of questions, the program i have needs a number but it can repeat and cant be in a pattern like 2,4,6 or 1,2,3. the 3 levels have different size array-lists one is somewhere between 1-27 the other is 1-50 and the last is 1-106 the range is always changing. i have confused myself thinking about this so any help would be great.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Break it down into small, independent parts. Get one part working by itself before moving on to the next part.

So what part are you having trouble with at the momment? If it's generating a random int in a range, look at the nextInt(int n) method in java.util.Random.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2849
    
  11

That really isn't very clear to me, but you can get a series of random elements from an array with something like:

Does that help?
James Eman
Ranch Hand

Joined: Mar 10, 2013
Posts: 45
ok yea i really have to organize my thoughts some how. i guess i need to be able to call a method and it will return a int but every time i call the method i will get a new int no repeating. dose that make more scene?? i have confused myself thinking how this needs to work i have this(below) but all it dose is freeze when its called.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2849
    
  11

It looks like you're going for a random permutation of numbers from 0 to gametype-1. Is that right? You could look at java.util.Collections and its shuffle(List) method, and see if that would help you.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11243
    
  16

James Eman wrote:ok yea i really have to organize my thoughts some how./quote]
THAT is indeed the first thing anyone who is writing a program needs to do. Coding is not 90% writing java and 10% thinking. Coding is 90% thinking and 10% writing java.

so...

i guess i need to be able to call a method and it will return a int but every time i call the method i will get a new int no repeating.

This isn't really the right way to start thinking about it. You're thinking in terms of java. You should start by thinking about things in English. Terms like 'call a method' and 'return an int' tell me you have already, possibly not consciously, decided how to do some things. I would start thinking like this

1) Have a list of questions for each 'level'
2) While on a given level, pick a question at random from the list
3) Present question to user
4) Get response from user
5) compute score
6) Repeat #2 through #5 until they finish the level, loose the game, or quit

so then you start looking at each individual step, and refining it. Make sub-steps and start spelling out what you want to do. Note that each of these steps are pretty much independent of each other. It doesn't matter where the question comes from - assuming I have it, presenting it to the user will always be done the same way.

so, for #2, which is where you seem to be stuck, think about it this way...you are kind of picking numbered balls out of a bag, and not putting the ball back in. So, the selections are random and cannot repeat. But note that this is exactly the same as lining up the balls in a random order, then reading them off sequentially. in other words, there are two ways to approach this:

1) put them all in order. Select one that is left randomly
2) randomize the order of all the things. Select them in order, sequentially.

#2 is what Greg is hinting at.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
James Eman
Ranch Hand

Joined: Mar 10, 2013
Posts: 45
ok i think i got want i need thanks for the help
 
wood burning stoves
 
subject: Random ArrayList