• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random number  RSS feed

 
siddharth sekhar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends

can you people suggest me which one should i pick of the following two , to generate a random value for a string array

int index = randomObject.nextInt(array.length);

or

int index = (int)(Math.random() * array.length);

i am new , so not have a lot of experience pl help

thank you
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No two ways about it. The first way is better. You can get nasty errors with the second method if you misplace the (), in which case it will always produce 0.

I think this is a continuation of your other thread and this discussion will probably be closed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!