• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generate Random List Length  RSS feed

 
Sammie Hassett
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
]Hello,
I have my code which will generate a random string using an ArrayList. However, I aso want to have it generate them in random lengths, not just using all the chars and numbers. Help please?


Thank you
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how to generate a random number? That's where you want to start. Once you can generate a random number, the next step is to create a list with an arbitrary length by adding elements X number of times, where X is an arbitrary number.
 
Sammie Hassett
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Do you know how to generate a random number? That's where you want to start. Once you can generate a random number, the next step is to create a list with an arbitrary length by adding elements X number of times, where X is an arbitrary number.


I kind of know how to do a random number for example like


but even that prints out number of the same length and I dont know really how to apply it to the list.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I say "Pick a number, any number" and you pick a number. Then I say, "See that pile of rocks over there? Go over to that pile of rocks and throw as many rocks as that number you just picked in that bucket over there."

That's kind of what you have to do. Do you know how to add things to a List?
 
Sammie Hassett
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:So if I say "Pick a number, any number" and you pick a number. Then I say, "See that pile of rocks over there? Go over to that pile of rocks and throw as many rocks as that number you just picked in that bucket over there."

That's kind of what you have to do. Do you know how to add things to a List?


Yes i know how to add to a list but i am still confused as to what you mean??
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pile of rocks ==> your array of letters

Bucket ==> a list that can hold any number of things

Pick a number, any number ==> generate a random number

Throw X rocks from the pile into that bucket => add (random number of whatever) from ? to ?

In your code:

You say that this always prints out the same length -- why do you think it does that? What is that length that's always the same right now? In your code, what controls how many times this loop executes? If you want to execute the loop X number of times, what do you change? If you want to execute X number of times where X is some arbitrary number what do you need to do?
 
Sammie Hassett
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Pile of rocks ==> your array of letters

Bucket ==> a list that can hold any number of things

Pick a number, any number ==> generate a random number

Throw X rocks from the pile into that bucket => add (random number of whatever) from ? to ?

In your code:

You say that this always prints out the same length -- why do you think it does that? What is that length that's always the same right now? In your code, what controls how many times this loop executes? If you want to execute the loop X number of times, what do you change? If you want to execute X number of times where X is some arbitrary number what do you need to do?


Im not sure...Im just starting, and I dont fully understand the code in depth..Im just coding what ive been taught so far
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sammie Hassett wrote:Im not sure...Im just starting, and I dont fully understand the code in depth..Im just coding what ive been taught so far

Ok, fair enough. You will have to make some effort to understand what the code is doing then. Read your code and understand why it is behaving the way it's behaving. One way to understand what's going on is to modify it a little bit:

Experiment with that, change some things and see what happens. Don't be afraid to experiment. Don't be afraid to make mistakes. It's by making mistakes that you learn.

Edit: and most of all, don't just rely on what you've been taught. The internet is full of examples and a lot of information can be found with a simple click of a button. Go look for it; Google is your friend.

Good luck.
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote: . . . don't just rely on what you've been taught. The internet is full of examples and a lot of information can be found with a simple click of a button. . . .
Never mind Google, there are far better places to find information like that. This is one place I would suggest.

Also find out about the newer features of Java®. If you search in that location I gave you, this is one of the things that come up. You may think it doesn't help, but lines 59‑60 show a new way to get ten million “random” numbers in an array. You can probably amend that to create a smaller array. In that instance, Titles.values().length evaluated to 8.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!