Jump to content

Random Elements From Array

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
Victor

Victor

    Programmer

  • Members
  • PipPipPipPip
  • 116 posts
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.

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
I have no idea what you mean.

#3
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
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]

~Aristotle said:

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 Posted Image

#4
Victor

Victor

    Programmer

  • Members
  • PipPipPipPip
  • 116 posts
I want to generate a random 8 digit string from an array

#5
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
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.

~Aristotle said:

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 Posted Image

#6
Victor

Victor

    Programmer

  • Members
  • PipPipPipPip
  • 116 posts
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
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
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
If my post was helpful, please help me build some rep Posted Image

#8
Victor

Victor

    Programmer

  • Members
  • PipPipPipPip
  • 116 posts
Yes please

#9
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
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]

~Aristotle said:

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 Posted Image