Here is an example on jsfiddle
var words = ["monitor", "program", "application", "keyboard", "javascript", "gaming", "network"];
var word = words[Math.floor(Math.random() * words.length)];
console.log(word);
document.getElementById("word").textContent = word;
And to have it fit in directly with you present code:
var getRandomWord = function () {
return words[Math.floor(Math.random() * words.length)];
};
Answer from Xotic750 on Stack OverflowGenerating a random word from an array
Create random Word/ Phrase generator using array - Stack Overflow
java - Random array-word generator - Stack Overflow
Random Word Generator w/ No Repeats
Is every word in English in your random word generator?
What is a random word?
What is the most random word?
Here is an example on jsfiddle
var words = ["monitor", "program", "application", "keyboard", "javascript", "gaming", "network"];
var word = words[Math.floor(Math.random() * words.length)];
console.log(word);
document.getElementById("word").textContent = word;
And to have it fit in directly with you present code:
var getRandomWord = function () {
return words[Math.floor(Math.random() * words.length)];
};
Try using it this way:
var getRandomWord = (function () {
var gameWordArray = [];
gameWordArray.push("monitor");
gameWordArray.push("program");
gameWordArray.push("application");
gameWordArray.push("keyboard");
gameWordArray.push("javascript");
gameWordArray.push("gaming");
gameWordArray.push("network");
return function () {
var randNum, finalWord;
randNum = Math.floor(Math.random() * gameWordArray.length);
finalWord = gameWordArray[randNum];
return finalWord;
};
})();
DEMO: http://jsfiddle.net/bCEFA/1/
Instead of declaring an array with a predefined length, you might as well declare an empty one and add values to the end of it (with .push()). You could've also declared the array like:
var gameWordArray = ["monitor", "program", ...];
You were trying to print word (which I renamed to getRandomWord), which was/is a function. You probably meant to use console.log(gameWordArray[randno]), which should work.
As the title says it, I'm dropping here the code to get a random word from an array as it hasn't been mentioned on any forum
Here is the code:
#include<stdio.h>
#include<stdlib.h>
int main(){
int x=0;
int y;
char words[][10]={"cat","dog","giraffe","fly"};
for(int i=0;i<10;i++){
y=rand()%4+1;
}
printf("%s ",words[y]);
return 0;
}
This code gives also a random number, which is "y".
If you run this code you might get the same answers twice or maybe more but that is because the array is small but the more your array contains more words, the chance of getting the same word decreases.
Also in case you want to enlarge the array just change the number "4" to the number of elements in the array.
Have a nice day everyone.
Use string equals method to compare string instead of ==
Replace the below condition
if(roll == name)
with
if(roll.equals(name))
String equals compares the contents of the string while == checks whether the two references point to the same memory objects or not.
Read this related post to learn more about string comparsions: Java String.equals versus ==
Unrelated, but instead of the chain of:
if (result == 0){
System.out.println(me[0]);
Consider something closer to:
System.out.println(me[result]);
roll==name will always return false because in that case you are checking whether they refer to the same memory location.
Use if (roll.equals(name)) instead.