Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a word randomly from list of other words?

Ok so i have an array of names like this

var names = ["Bob", "Aaron", "John"];

How would i have a line or lines of code that would randomly pick between all 3 of these names and display it? Would i use math.random()?

like image 492
awoldt Avatar asked Jul 27 '15 03:07

awoldt


People also ask

How do you randomize a word?

To generate random text using the current language in a Word document and customize the number of paragraphs and sentences: Position the cursor in the document where you want to generate random text. Type =RAND(number of paragraphs, number of sentences) such as =RAND(3,2). Press Enter.


1 Answers

Since you have a collection of names rather than key value pairs it's better to use an array to store your names rather than a hash.

With an array, you can use Math.floor and Math.random to generate an index to look up in the names array.

names[Math.floor(Math.random() * names.length)]
like image 62
Pavan Ravipati Avatar answered Oct 02 '22 08:10

Pavan Ravipati