• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Random

 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have requirement like
I have 40 files names are 00.txt,01.txt....40.txt. I need to select this files randomly. Math.random is helpless to me
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Meet,

why is Math.random() helpless in this case??

There are probably different ways how you get to a specific file but in any case you will need something to produce random values. Why not use Math.random() for this?

Marco
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it helpless? Couldn't you use it to generate a random number, concat that to a .txt extension, then use that to select the filename you want to open?
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco Ehrentreich wrote:why is Math.random() helpless in this case??

There are probably different ways how you get to a specific file but in any case you will need something to produce random values. Why not use Math.random() for this?

I think Meet has problems transforming the double to an int.

It's quite easy though. To return a number between A (inclusive) and B (exclusive), the calculation is as follows: In your case this would be Math.random() * 41. Note the 41 - Math.random() will never return 1.0, so Math.random() * 41 will always be smaller than 41. Cast it to int, and the maximum value returned is 40.

But you'll probably find java.util.Random and its nextInt() method a lot easier.
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you might want to have a look at java.util.Random. It has methods to return a value between 0 and a specified number.
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am getting some repeated numbers. Even I tried Random class too.




OutPut
-------
5
5
7
5
7
6
3
2
5
4
9
 
Maneesh Godbole
Saloon Keeper
Posts: 11177
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you want random and unique numbers?
In that case you will have to implement the algorithm to ensure unique numbers yourself. Check out the Set API
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or perhaps a List in combination with Collections.shuffle.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50182
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randomness implies the possibility of repeated values. You have already had two useful suggestions; did you get them to work?
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally Shuffle method resolves my issue. Added all the files names in the arrayList and shuffled using collections class.


Thanks for all your reply.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic