从String数组中选择随机元素

时间:2015-08-03 16:03:40

标签: java random

function truncate_filenames () {
  for f in *.png *.jpg *.tif; do
      if [[ -f $f ]]; then
          ext="${f:(-3)}"
          if [[ ! ${f%.*} =~ .*C[0-9]{2}P[0-9]{2}$ ]]; then
              mv -v "$f" "$(echo "$f" | grep -Eo '.*C[0-9]{2}P[0-9]{2}').$ext"
          fi
      fi
  done
}

我想从数组中随机选择一个元素,并将随机选择分配给变量public class Elements { public static String element_answer = ""; public String[] elements = new String[118]; elements[0] = "Hydrogen"; elements[1] = "Helium"; elements[2] = "Lithium"; elements[3] = "Beryllium"; elements[4] = "Boron"; (...) elements[117] = "Ununoctium"; } 。怎么可以这样做?

4 个答案:

答案 0 :(得分:1)

试试这个:

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdlib.h>

int fd, fd2;
struct in_addr interface_addr;
int addr_size;
int optval = 1, recv_len = 4;
char *remote_ip = "224.2.0.6", *remote_ip2 = "224.2.0.7";
char *remote_port = "6666";
char *buf = "conn", *buf2 = "send";
struct addrinfo *remote_address_info, *remote_address_info2;

int main() {
  // create first and second socket
  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket() failed");
  }
  if ((fd2 = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket2() failed");
  }

  // set socket options on both
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval) < 0) {
    perror("setsockopt(SO_REUSEADDR) failed");
  }
  if (setsockopt(fd2, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval) < 0) {
    perror("setsockopt2(SO_REUSEADDR) failed");
  }

  addr_size = sizeof(interface_addr);

  // structure the addr
  if (getaddrinfo(remote_ip, remote_port, NULL, &remote_address_info) != 0) {
    perror("getaddrinfo(remote_ip) failed");
  }
  if (getaddrinfo(remote_ip2, remote_port, NULL, &remote_address_info2) != 0) {
    perror("getaddrinfo2(remote_ip) failed");
  }

  // connect ON FIRST SOCKET ONLY
  if (connect(fd, remote_address_info->ai_addr, remote_address_info->ai_addrlen) != 0) {
    perror("connect() failed");
  }

  // sendto first and second
  // same result if use send() or sendto() on first socket
  if (sendto(fd, buf, recv_len, 0, remote_address_info->ai_addr, remote_address_info->ai_addrlen) == -1) {
    perror("sendto() failed");
  }
  if (sendto(fd2, buf2, recv_len, 0, remote_address_info2->ai_addr, remote_address_info2->ai_addrlen) == -1) {
    perror("sendto2() failed");
  }
}

答案 1 :(得分:1)

你可以这样做,

Random random = new Random();
element_answer = elements[random.nextInt(elements.length)];

答案 2 :(得分:-1)

您可以使用:

import java.util.Collections;
[...]
Collection.shuffle(elements);
element_answer = elements[0];

它会成功。你将整个列表洗牌并取出第一个列表,这个列表现在是这个列表中的一些元素。由Java Collections随机化。

答案 3 :(得分:-1)

我想你的ESP(笑话)的意图是 A Class,它返回一个从字符串数组中随机选择的字符串(化学元素的名称)。如果是这样,下面是一个可能的工具。

Elements.java:

// The Class name of Java starts with upper case as normal
public class Elements {
    // There is no need of this field value.
    // public static String element_answer = "";

    public String[] elements = new String[118];

    // You need a random object
    private Random random = new Random();

    // Inside Constructor you can instantiate Strings in the Array
    public Elements() {
        // Array number starts from '0'.
        // This is quite basic knowledge. 
        elements[0] = "Hydrogen";
        elements[1] = "Helium";
        elements[2] = "Lithium";
        elements[3] = "Beryllium";
        elements[4] = "Boron";
        (...)
        elements[117] = "Ununoctium";
        // So array number starts from '0'
        // that the last number is [array length] - 1
    }

    // You need to define a method.
    // This is basic idea for Java Programmers!
    public String getElementRandom() {
        return elements[random.nextInt(elements.length)];
    }
}

Activity(或某处)内的用法是:

 // Declare at field
 Elements elements = new Elements();

 (...)

 // somewhere you want to use
 p1_button1.setText(elements.getElementRandom());