• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Random ArrayList

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
James Eman
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok i think i got want i need thanks for the help
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic