Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 08-13-2007, 09:29 PM
Member
 
Join Date: Aug 2007
Posts: 2
acidacid is on a distinguished road
generating random numbers in a 5x5 array.
hello. :]

so, I've been trying to figure out for the longest time how to generate numbers in my 5x5 matrices without having the numbers repeat themselves more than once.

Code:
void Card() { salida = ""; salida2 = ""; for (i = 0; i <= 4; i++) { for (j = 0; j <= 4; j++) { card1[i][j] = 1 + (int) (Math.random() * 99); if (card1[i][j] <= 9) { cadena = "0" + card1[i][j]; } else { cadena = "" + card1[i][j]; } salida = salida + " " + cadena; if (j == 4) { salida = salida + "\n"; } } } JOptionPane.showMessageDialog(null, salida, "Card 1", JOptionPane.INFORMATION_MESSAGE); }
this what I have for the array. I've had plenty more, but I tried to many different methods to solve my problem, and none of it really helped. your help would be greatly appreciated. :]

oh and I apologize about the spanish variables, but i'm still sure the code still is understandable. :]
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-13-2007, 10:45 PM
Member
 
Join Date: Aug 2007
Posts: 30
dmacvittie is on a distinguished road
Since this all looks okay, I presume it is the non-repeating part you're struggling with?

The best solution to that problem AFAIK is brute force - write a method that loops through the list and returns true if it is a duplicate or false if not. put that routine and the generation of the next random into a loop until it returns false, and only then assign the value into the array.
Code:
boolean isDuplicate = true; while(isDuplicate) { card[i][j]=1 + (int) (Math.random() *99); isDuplicate = checkDuplicate(card1, i, j); }
Then the isDuplicate method has to walk the array and compare the element at i,j with everything until it either finds a match (returning true) or runs out of elements (returning false).

No worries about variable names . I don't apologize for making you read them in English, you shouldn't for returning the favor.

Hope that helps,
Don.
__________________
Don MacVittie F5 Networks -
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
  #3 (permalink)  
Old 08-14-2007, 05:07 AM
Member
 
Join Date: Aug 2007
Posts: 2
acidacid is on a distinguished road
Quote:
Originally Posted by dmacvittie View Post
Code:
boolean isDuplicate = true; while(isDuplicate) { card[i][j]=1 + (int) (Math.random() *99); isDuplicate = checkDuplicate(card1, i, j); }
umm... checkDuplicate is giving me a "cannot resolve symbol." is there any way I can initialize it or declare it as a variable or something? : D

other than that I understood everything perfectly, and I thank you very much for the help you provided. <333
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-14-2007, 05:44 AM
Member
 
Join Date: Aug 2007
Posts: 30
dmacvittie is on a distinguished road
You have to write it, just iterate through the array and look for a match with the new number.

Don.
__________________
Don MacVittie F5 Networks -
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
random numbers without random class` carlos123 New To Java 4 01-18-2008 12:44 AM
random numbers carlos123 New To Java 1 12-22-2007 04:56 AM
Adding numbers in a 2 dimensional array j0shizabeast New To Java 2 11-27-2007 06:31 AM
random numbers without using java funtions carlos123 New To Java 8 11-16-2007 12:13 PM
Adding numbers in array Shaolin New To Java 1 11-15-2007 08:30 PM


All times are GMT +3. The time now is 11:27 AM.


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