Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Java Help

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-24-2007, 12:55 AM
Victor Victor is offline
Learning Programmer
 
Join Date: May 2007
Posts: 96
Rep Power: 5
Victor is on a distinguished road
Default Random Elements From Array

Just wondering, how do you get x number of elements (pseudo randomly) from an array and set it to a string. I've been Googling but its either random numbers or something like it.
__________________
Pegasus Homepage
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 11-24-2007, 12:04 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,736
Last Blog:
Passwords
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default

I have no idea what you mean.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-24-2007, 08:50 PM
gszauer's Avatar   
gszauer gszauer is offline
Programmer
 
Join Date: Nov 2007
Location: Florida
Age: 18
Posts: 113
Rep Power: 3
gszauer is on a distinguished road
Default

Im not clear on what you mean eather....
Please provide your array definition.
Im not shure what you mean, but if you are looking to pull a random element from a string of arrays...

Java5 Code:
  1. public class RandomElement {
  2.   public static int arrayLength, randomElement;
  3.   // Use Array Length to set the length of the array
  4.  
  5.   public static void main (String[] args){
  6.     String[] myArray = [arrayLength];
  7.     // Code to populate array
  8.     randomElement = (int)(Math.random() * (myArray.length - 1));
  9.     System.out.println(myArray[randomElement]);
  10.   }
  11. }
__________________
Quote:
Originally Posted by ~Aristotle
It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-27-2007, 03:50 PM
Victor Victor is offline
Learning Programmer
 
Join Date: May 2007
Posts: 96
Rep Power: 5
Victor is on a distinguished road
Default

I want to generate a random 8 digit string from an array
__________________
Pegasus Homepage
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 11-27-2007, 05:20 PM
gszauer's Avatar   
gszauer gszauer is offline
Programmer
 
Join Date: Nov 2007
Location: Florida
Age: 18
Posts: 113
Rep Power: 3
gszauer is on a distinguished road
Default

This should do it mate:
Java5 Code:
  1. public class GenerateString {
  2.   private static int[] myArray = {5, 2, 6, 9, 8, 7, 4, 1, 2, 3, 7, 5, 6, 8, 5, 8, 7, 0};
  3.   private static int randomNum, randStrLen = 8;
  4.   private static String random;
  5.  
  6.   public static String generateStr(){
  7.     random = "";
  8.     for (int i = 0; i < randStrLen; i++){
  9.         randomNum = (int)(Math.random() * (myArray.length - 1));
  10.         random += myArray[randomNum];
  11.     }
  12.     return random;
  13.   }
  14.  
  15.   public static void main (String[] args){
  16.     System.out.println(generateStr());
  17.   }
  18. }

Or if you want a re-usable function try this:
Java5 Code:
  1. public class GenerateString {
  2.  
  3.   // The generateStr function takes 2 arguments
  4.   // and integer array for the array to generate the number from
  5.   // and an integer, wich is the length of the string to be generated
  6.   // this function can be called like so: generateStr(myArray, 8)
  7.   // this function returns a String value
  8.   public static String generateStr(int[] arrayTarget, int randStrLen){
  9.     int randomNum;
  10.     String random = "";
  11.     for (int i = 0; i < randStrLen; i++){
  12.         randomNum = (int)(Math.random() * (arrayTarget.length - 1));
  13.         random += arrayTarget[randomNum];
  14.     }
  15.     return random;
  16.   }
  17.  
  18.   public static void main (String[] args){
  19.     int[] myArray = {5, 2, 6, 9, 8, 7, 4, 1, 2, 3, 7, 5, 6, 8, 5, 8, 7, 0};
  20.     System.out.println(generateStr(myArray, 8));
  21.   }
  22. }

A nother option would be to create an object... But i dont think it would be practical.
__________________
Quote:
Originally Posted by ~Aristotle
It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep

Last edited by gszauer; 11-27-2007 at 05:31 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 11-29-2007, 04:29 PM
Victor Victor is offline
Learning Programmer
 
Join Date: May 2007
Posts: 96
Rep Power: 5
Victor is on a distinguished road
Default

I tried your method and its works, but I do need to be able to get it as a string because I need to display it in a GUI and write it to a file.
__________________
Pegasus Homepage
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 11-29-2007, 05:34 PM
gszauer's Avatar   
gszauer gszauer is offline
Programmer
 
Join Date: Nov 2007
Location: Florida
Age: 18
Posts: 113
Rep Power: 3
gszauer is on a distinguished road
Default

The second method does that, would you like some sample code of how to use it?
__________________
Quote:
Originally Posted by ~Aristotle
It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 12-01-2007, 08:10 PM
Victor Victor is offline
Learning Programmer
 
Join Date: May 2007
Posts: 96
Rep Power: 5
Victor is on a distinguished road
Default

Yes please
__________________
Pegasus Homepage
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 12-01-2007, 09:38 PM
gszauer's Avatar   
gszauer gszauer is offline
Programmer
 
Join Date: Nov 2007
Location: Florida
Age: 18
Posts: 113
Rep Power: 3
gszauer is on a distinguished road
Default

The below example should demonstrate.
Note that the first number will be output in the console. the rest is a gui

Java5 Code:
  1. import javax.swing.*;
  2.  
  3. public class GenerateString {
  4.  
  5.   public static String generateStr(int[] arrayTarget, int randStrLen){
  6.     int randomNum;
  7.     String random = "";
  8.     for (int i = 0; i < randStrLen; i++){
  9.         randomNum = (int)(Math.random() * (arrayTarget.length - 1));
  10.         random += arrayTarget[randomNum];
  11.     }
  12.     return random;
  13.   }
  14.  
  15.   public static void main (String[] args){
  16.     int[] myArray = {5, 2, 6, 9, 8, 7, 4, 1, 2, 3, 7, 5, 6, 8, 5, 8, 7, 0};
  17.     int arrayLength;
  18.     String input;
  19.     String s1, s2, s3;
  20.    
  21.     s1 = "Random string 1: "+generateStr(myArray, 8);
  22.     s2 = "Random string 2: "+generateStr(myArray, 8);
  23.    
  24.     System.out.println(s1);
  25.     JOptionPane.showMessageDialog(null, s2, "Random String", JOptionPane.PLAIN_MESSAGE);
  26.    
  27.     input = JOptionPane.showInputDialog("Please enter string length (no more than 15)");
  28.     arrayLength = Integer.parseInt(input);
  29.     s3 = "Random string 3: "+generateStr(myArray, arrayLength);
  30.     JOptionPane.showMessageDialog(null, s3, "Random String", JOptionPane.PLAIN_MESSAGE);
  31.   }
  32. }
__________________
Quote:
Originally Posted by ~Aristotle
It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB 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
Help regarding the sorting of arrays with flags! Yuriy M C and C++ 3 10-12-2007 10:30 PM
HELP! 3x3 array random number 1-9 no repeat siren C and C++ 3 09-25-2007 02:03 PM
fread into array position kenna C and C++ 0 08-17-2007 08:03 AM
Python 2D array question annannienann Python 3 04-23-2007 04:36 PM
Moving elements in an array Fischerspooner General Programming 9 12-12-2006 08:24 PM


All times are GMT -5. The time now is 11:59 PM.