• 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

Java Random

 
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or perhaps a List in combination with Collections.shuffle.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic