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.
Random Elements From Array
Started by Victor, Nov 23 2007 10:55 PM
8 replies to this topic
#1
Posted 23 November 2007 - 10:55 PM
|
|
|
#2
Posted 24 November 2007 - 10:04 AM
#3
Posted 24 November 2007 - 06:50 PM
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...
[HIGHLIGHT="Java5"]public class RandomElement {
public static int arrayLength, randomElement;
// Use Array Length to set the length of the array
public static void main (String[] args){
String[] myArray = [arrayLength];
// Code to populate array
randomElement = (int)(Math.random() * (myArray.length - 1));
System.out.println(myArray[randomElement]);
}
}[/HIGHLIGHT]
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...
[HIGHLIGHT="Java5"]public class RandomElement {
public static int arrayLength, randomElement;
// Use Array Length to set the length of the array
public static void main (String[] args){
String[] myArray = [arrayLength];
// Code to populate array
randomElement = (int)(Math.random() * (myArray.length - 1));
System.out.println(myArray[randomElement]);
}
}[/HIGHLIGHT]
~Aristotle said:
It is the mark of an educated mind to entertain a tought without accepting it
#4
Posted 27 November 2007 - 01:50 PM
#5
Posted 27 November 2007 - 03:20 PM
This should do it mate:
[HIGHLIGHT="Java5"]public class GenerateString {
private static int[] myArray = {5, 2, 6, 9, 8, 7, 4, 1, 2, 3, 7, 5, 6, 8, 5, 8, 7, 0};
private static int randomNum, randStrLen = 8;
private static String random;
public static String generateStr(){
random = "";
for (int i = 0; i < randStrLen; i++){
randomNum = (int)(Math.random() * (myArray.length - 1));
random += myArray[randomNum];
}
return random;
}
public static void main (String[] args){
System.out.println(generateStr());
}
}[/HIGHLIGHT]
Or if you want a re-usable function try this:
[HIGHLIGHT="Java5"]public class GenerateString {
// The generateStr function takes 2 arguments
// and integer array for the array to generate the number from
// and an integer, wich is the length of the string to be generated
// this function can be called like so: generateStr(myArray, 8)
// this function returns a String value
public static String generateStr(int[] arrayTarget, int randStrLen){
int randomNum;
String random = "";
for (int i = 0; i < randStrLen; i++){
randomNum = (int)(Math.random() * (arrayTarget.length - 1));
random += arrayTarget[randomNum];
}
return random;
}
public static void main (String[] args){
int[] myArray = {5, 2, 6, 9, 8, 7, 4, 1, 2, 3, 7, 5, 6, 8, 5, 8, 7, 0};
System.out.println(generateStr(myArray, 8));
}
}[/HIGHLIGHT]
A nother option would be to create an object... But i dont think it would be practical.
[HIGHLIGHT="Java5"]public class GenerateString {
private static int[] myArray = {5, 2, 6, 9, 8, 7, 4, 1, 2, 3, 7, 5, 6, 8, 5, 8, 7, 0};
private static int randomNum, randStrLen = 8;
private static String random;
public static String generateStr(){
random = "";
for (int i = 0; i < randStrLen; i++){
randomNum = (int)(Math.random() * (myArray.length - 1));
random += myArray[randomNum];
}
return random;
}
public static void main (String[] args){
System.out.println(generateStr());
}
}[/HIGHLIGHT]
Or if you want a re-usable function try this:
[HIGHLIGHT="Java5"]public class GenerateString {
// The generateStr function takes 2 arguments
// and integer array for the array to generate the number from
// and an integer, wich is the length of the string to be generated
// this function can be called like so: generateStr(myArray, 8)
// this function returns a String value
public static String generateStr(int[] arrayTarget, int randStrLen){
int randomNum;
String random = "";
for (int i = 0; i < randStrLen; i++){
randomNum = (int)(Math.random() * (arrayTarget.length - 1));
random += arrayTarget[randomNum];
}
return random;
}
public static void main (String[] args){
int[] myArray = {5, 2, 6, 9, 8, 7, 4, 1, 2, 3, 7, 5, 6, 8, 5, 8, 7, 0};
System.out.println(generateStr(myArray, 8));
}
}[/HIGHLIGHT]
A nother option would be to create an object... But i dont think it would be practical.
~Aristotle said:
It is the mark of an educated mind to entertain a tought without accepting it
#6
Posted 29 November 2007 - 02:29 PM
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.
#7
Posted 29 November 2007 - 03:34 PM
The second method does that, would you like some sample code of how to use it?
~Aristotle said:
It is the mark of an educated mind to entertain a tought without accepting it
#8
Posted 01 December 2007 - 06:10 PM
#9
Posted 01 December 2007 - 07:38 PM
The below example should demonstrate.
Note that the first number will be output in the console. the rest is a gui
[HIGHLIGHT="Java5"]import javax.swing.*;
public class GenerateString {
public static String generateStr(int[] arrayTarget, int randStrLen){
int randomNum;
String random = "";
for (int i = 0; i < randStrLen; i++){
randomNum = (int)(Math.random() * (arrayTarget.length - 1));
random += arrayTarget[randomNum];
}
return random;
}
public static void main (String[] args){
int[] myArray = {5, 2, 6, 9, 8, 7, 4, 1, 2, 3, 7, 5, 6, 8, 5, 8, 7, 0};
int arrayLength;
String input;
String s1, s2, s3;
s1 = "Random string 1: "+generateStr(myArray, 8);
s2 = "Random string 2: "+generateStr(myArray, 8);
System.out.println(s1);
JOptionPane.showMessageDialog(null, s2, "Random String", JOptionPane.PLAIN_MESSAGE);
input = JOptionPane.showInputDialog("Please enter string length (no more than 15)");
arrayLength = Integer.parseInt(input);
s3 = "Random string 3: "+generateStr(myArray, arrayLength);
JOptionPane.showMessageDialog(null, s3, "Random String", JOptionPane.PLAIN_MESSAGE);
}
}[/HIGHLIGHT]
Note that the first number will be output in the console. the rest is a gui
[HIGHLIGHT="Java5"]import javax.swing.*;
public class GenerateString {
public static String generateStr(int[] arrayTarget, int randStrLen){
int randomNum;
String random = "";
for (int i = 0; i < randStrLen; i++){
randomNum = (int)(Math.random() * (arrayTarget.length - 1));
random += arrayTarget[randomNum];
}
return random;
}
public static void main (String[] args){
int[] myArray = {5, 2, 6, 9, 8, 7, 4, 1, 2, 3, 7, 5, 6, 8, 5, 8, 7, 0};
int arrayLength;
String input;
String s1, s2, s3;
s1 = "Random string 1: "+generateStr(myArray, 8);
s2 = "Random string 2: "+generateStr(myArray, 8);
System.out.println(s1);
JOptionPane.showMessageDialog(null, s2, "Random String", JOptionPane.PLAIN_MESSAGE);
input = JOptionPane.showInputDialog("Please enter string length (no more than 15)");
arrayLength = Integer.parseInt(input);
s3 = "Random string 3: "+generateStr(myArray, arrayLength);
JOptionPane.showMessageDialog(null, s3, "Random String", JOptionPane.PLAIN_MESSAGE);
}
}[/HIGHLIGHT]
~Aristotle said:
It is the mark of an educated mind to entertain a tought without accepting it


Sign In
Create Account


Back to top









