aspose file tools*
The moose likes Beginning Java and the fly likes Java Random Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Random" Watch "Java Random" New topic
Author

Java Random

Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
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

Joined: Mar 07, 2007
Posts: 1282

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

Joined: Feb 10, 2009
Posts: 710
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?


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
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

Joined: Oct 08, 2008
Posts: 492
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

Joined: Jul 26, 2007
Posts: 10451
    
    8

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Or perhaps a List in combination with Collections.shuffle.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Randomness implies the possibility of repeated values. You have already had two useful suggestions; did you get them to work?
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Finally Shuffle method resolves my issue. Added all the files names in the arrayList and shuffled using collections class.


Thanks for all your reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Random