samedi 30 novembre 2019

lua : storing values in string array and printing a value by calling a random index

i have a table named t

t = {"hi","hello","hola","whats up"}

and i want to be able to index the elements so that

hi = 1
hello = 2
hola = 3
whats up = 4

so that this random number generator

math.randomseed(os.time())
ranNumber = math.random(4)

can call on the tables elements and have a 25% probability of printing one element

if you dont understand, the java equivalent of this program would work like this

import java.awt.Desktop;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import javax.xml.soap.SOAPElement;

public class PrintRandom{

 public static void main(String []args){

    String[] names={"hi", "hello", "hola", "whats up"};
    Random r=new Random();
    int RandomNumber=r.nextInt(4);

    System.out.println(names[RandomNumber]);
 }
}



Aucun commentaire:

Enregistrer un commentaire