Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-03-2008, 10:09 AM
Member
 
Join Date: Mar 2008
Posts: 8
googgoo is on a distinguished road
random string are duplicate
I create mathod random() for random a string.
When run this code below sometimes word at random are duplicate as:
word: ANT
word: ANT
I require random word and not duplicate.
How to solve this problem?

Code:
class Test{ static String[] STRING = {"ANT","COW","HAM","BAT","DOG","CAT","MIN","MAX","JOB","CAR","WIN"}; static final Random random = new Random(); // // random(STRING); public static void random(String[] STRING){ for (int i = 0; i < 1;i++) { num = random.nextInt(STRING.length); System.out.println("random value: "+num); System.out.println("word : "+STRING[num]); } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-03-2008, 10:43 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,104
hardwired is on a distinguished road
Code:
import java.util.*; public class Test { static String[] STRING = { "ANT","COW","HAM","BAT","DOG","CAT","MIN","MAX","JOB","CAR","WIN" }; static final Random random = new Random(); public static void main(String[] args) { int[] nums = new int[6]; Arrays.fill(nums, -1); for (int i = 0; i < nums.length; i++) { int num = getNumber(nums); nums[i] = num; System.out.println("random value: "+num); System.out.println("word : "+STRING[num]); } } private static int getNumber(int[] array) { int n; do { n = random.nextInt(STRING.length); } while(contains(array, n)); return n; } private static boolean contains(int[] array, int n) { for(int i = 0; i < array.length; i++) { if(array[i] == n) return true; } return false; } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-03-2008, 10:53 AM
Member
 
Join Date: Mar 2008
Posts: 8
googgoo is on a distinguished road
Thank you very much.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-03-2008, 11:01 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,875
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
It can be happened according to your code. In your for loop, after firing the first turn. It print a String and exit from it. Say first time you got the String "ANT". When you call the random() method next time, there is no way to keep the previous output. So even in the next time, you can get the same String there.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Get a random String LeoCoderIV New To Java 6 04-07-2008 03:58 PM
How to make a hashmap to allow duplicate values? Preethi New To Java 0 02-08-2008 01:35 PM
random numbers without random class` carlos123 New To Java 4 01-17-2008 11:44 PM
How to prevent duplicate username entry in database? anki1234 JavaServer Pages (JSP) and JSTL 4 01-09-2008 09:02 AM
Duplicate entry in registration form!!! anki1234 Advanced Java 1 01-04-2008 09:15 PM


All times are GMT +3. The time now is 01:53 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org