How to choose a random string out of an array?

pick random string from array javascript
select random string from array java
c# pick random string from list
get random element from array java
get random elements from array javascript
javascript pick multiple random from array
c# get n random items from list
c# random array element

I am trying to choose a random word from an array of strings but cannot figure out how.

I have researched on this and here is what I have so far:

roast.js
const roastList = [
    'Apples',
    'Bananas',
    'Pears',
  ];

  const roast = roastList[Math.floor(Math.random() * roastList.length)];

module.exports = {
    roast
};
index.js:
case 'roast':
        if (!message.mentions.users.size) {
            return message.reply('you need to tag a user in order to roast them!');
        }

        message.channel.send(`Hey ${taggedUser}, ${roast}`);
        break;

I copied the code so the fruits are placeholders. I hope that I can get different fruits each time I output roast.

The problem here is that the module code will only be run one time, that means that only one "roast" will be chosen the first time the module is loaded. You want to wrap up the "roast" selection logic in a function that can be called every time you want to "roast" someone.

Consider making roast.js export a function:

const roastList = [
    'Apples',
    'Bananas',
    'Pears'
];

module.exports = function () {
    return roastList[Math.floor(Math.random() * roastList.length)];
};

Then call that function in your template:

const roast = require('./roast.js');
...
message.channel.send(`Hey ${taggedUser}, ${roast()}`);

Select Random String From An Array In .NET Core and C#, You only need to generate a random number that acts as the index value for String array. Random;. public class RandomStringFromArray. {. public static void main(String[] args) System.out.println(arr[randomNumber]);. }. It is quite easy. You only need to generate a random number that acts as the index value for String array. You can generate random value using Random class defined in java.util package.

Works for me. With the export you have defined in your example the import can be done like the following, for example.

const roast = require('./roast').roast;

If you simply export the string instead of an object containing the string, you can also do this as follows:

module.exports = roast;
...

const roast = require('./roast');

See also my repl.it for demonstration

EDIT: I have just noticed by the comments you have posted that you are probably looking for a solution where you can get a new random roast each time when a roaster is required in the chat protocol. To do this my suggestion is to export a function which returns a random roast string. See example below, I have also extended the repl.it demo.

roast3.js

const roastList = [
    'Apples',
    'Bananas',
    'Pears',
  ];

function roast() {
  return roastList[Math.floor(Math.random() * roastList.length)]
}

module.exports = {
  roast
};

index.js

const roast3 = require('./roast3').roast;
for (x=0; x<10; x++) {
  console.log(roast3())
}

How to pick up a random string from an array of strings in Java, Using just a single line of code, learn how to randomly pick an item from an array. Your parents haven't even heard of these shows, so they can't help you out� We can use the random number generator to pick a random item from an array. The following code snippet has an array of author names (strings). We can pick a random author by generating a random number that is less than the number of items in the array and use the random index to pick a random author name in the string.

const roastList = [
  'Apples',
  'Bananas',
  'Pears',
];

const roast = roastList[Math.floor(Math.random() * roastList.length)];
console.log(roast);

This actually works. But why do you exports it ?

Picking a Random Item from an Array, Pick a random value from multiple lists; Choose a random element from a multidimensional array; Choose the same element from the list every� It is programmers need to choose or select or get a random element or random index of an Array or ArrayList in Java. The random element may be a number or string. Let us explore Math.random() method with examples. The same code can be used to implement a Lottery Draw to pick a random contestant from a list of participants.

Python random.choice() to choose random element from list, String , Pick a random string from a string array C#. Hello,. I know that this has probably been asked many times but I spent 1 hour to find a solution in� The task is to select the random element from the array using JavaScript. Approach 1: Use Math.random() function to get the random number between(0-1, 1 exclusive). Multiply it by the array length to get the numbers between(0-arrayLength). Use Math.floor() to get the index ranging from(0 to arrayLength-1).

Pick a random string from a string array C#, Because that's exactly what this article is about! With a random number you can pick a random element from an array, pick a winner from a list, generate dice rolls ,� Let us first declare a string array and initialize −String[] strArr = { P, Q, R, S,T, U, V, W };Now, create a Random object −Random rand = n

How to Generate Random Numbers & Strings in Ruby, var list = new List<string>{ "one","two","three","four"};. Now get the count of the elements and display randomly. int index = random.Next(list.Count);� Printing 2D Array [[11 22 33] [44 55 66] [77 88 99]] Choose random row from a 2D array [44 55 66] Random number from random row is 66 Note : Above all, examples are not cryptographically secure . If you are doing this for any security-sensitive application then to cryptographically secure random output, use random.SystemRandom().choice instead

Comments
  • Please edit your post to include the code that consumes the roast export.
  • I only get the output, [object Object] as the output when using this code.
  • Did you copy the code verbatim? Then it should work as shown in my repl.it. Note, roast3 is function. So you need to make a function call when using it, e.g., console.log(roast3()) or ${roast3()}
  • I import it to index.js and use it there. Just keeps the code a bit more clean and tidy.
  • imgur.com/a/wS8Wb54 is proof it only gives one output. imgur.com/a/nRdNsHi This is the code in index.js
  • @OscarHurst, see EDIT on my answer