2013-02-19 37 views
1

所以我所拥有的是http://garrettstelly.com,它在启动时吐出了二十个术语之一。代码非常重,我很好。(JavaScript)随机文本输出 - 建议?

我打算做一个网站随机吐出一个名字,问题是我可以有无穷无尽的名字。对于我目前使用的js,我使用随机滚动,甚至是0到1之间的部分来读取名称。问题是我不能只添加一个短语,我不得不一次添加一个块,以使概率变得均匀。

我该如何制作一个脚本,并且每次只能添加一个可能性?

这里是为garrettstelly.com的JavaScript:

var roll = Math.random() 
if (roll<0.05) 
{document.write('<a href="http://www.facebook.com/abroheem.vonclinxenburg">Bro-Heem</a>');} 
else if (roll<0.10) 
{document.write('I am too white for my own good');} 
else if (roll<0.15) 
{document.write('I love the way you paste those stickers.');} 
else if (roll<0.20) 
{document.write('You probably were not just thinking about Wichita');} 
else if (roll<0.25) 
{document.write('Yummy, Adhesive!');} 
else if (roll<0.30) 
{document.write('<a href="http://www.rolex.com/">Rolex</a>');} 
else if (roll<0.35) 
{document.write('There is a 5% chance that you will see this when you first visit this website.');} 
else if (roll<0.40) 
{document.write('Making Money.<br>Choppas How We Do Today.');} 
else if (roll<0.45) 
{document.write('45, get your bills roll em high.');} 
else if (roll<0.50) 
{document.write('I WILL teach you how to fish');} 
else if (roll<0.55) 
{document.write('I am a gangsta.');} 
else if (roll<0.60) 
{document.write('Please get out of my website');} 
else if (roll<0.65) 
{document.write('<a href="http://www.facebook.com/luke.immel?fref=ts">derriere</a>');} 
else if (roll<0.70) 
{document.write('I think YOU are a Q T PIE');} 
else if (roll<0.75) 
{document.write('X=Fries');} 
else if (roll<0.80) 
{document.write("Idle hands are the Devil's playground.<br>The Devil is smaller than hands.");} 
else if (roll<0.85) 
{document.write('I am about to be late for class');} 
else if (roll<0.90) 
{document.write('"Hipster"');} 
else if (roll<0.95) 
{document.write('I am late for class');} 
else 
{document.write('Please refrain from drinking the water located within the wishing well, thank you.');} 
+0

这与JavaScript无关。使用一个数组,除以它的长度,并将其与你的滚动进行比较。这很简单,试图弄清楚。 – SoonDead 2013-02-19 16:29:18

+2

@SoonDead但是提供的示例代码是用JavaScript编写的......所以这个问题与JavaScript有关。 – guypursey 2013-02-19 16:35:15

回答

2

存放在数组中的可能性,并获取数组的一个随机元素:

Demo

function getRandomName() 
{ 
    var names = [ 
     'John', 
     'Sue', 
     'Bob', 
     'Sandeep' 
    ]; 

    return names[Math.floor(Math.random() * names.length)]; 
} 

document.write(getRandomName()); 
0

我会用一个数组:

var outputTextArr = ['text 1', 'text 2', 'text 3', 'text 4', 'more text', 'some text']; 

然后随机目标数组中的项目:

function randomRange(from, to){ 
    return Math.floor(Math.random() * (to - from + 1) + from); 
} 

var outputText = outputTextArr[randomRange(0, outputTextArr.length - 1)];//var output text will have a random item 
1

你应该把你想要吐出的所有东西都放到一个数组中。例如:

var arr = [ 
    '<a href="http://www.facebook.com/abroheem.vonclinxenburg">Bro-Heem</a>', 
    'I am too white for my own good', 
    // other entries... 
    'Please refrain from drinking the water located within the wishing well, thank you.' 
]; 

那么您辊仍应Math.random()但乘以由阵列的长度:通过使用document.write

var roll = (Math.floor(Math.random()) * arr.length); 

然后用合适的数组索引写你的结果,你的情况(尽管可能有更好的方法):

document.write(arr[roll]); 

现在你可以添加到你的数组,只要你喜欢。

0

像这样的东西应该工作

var sentences = 
[ 
    "I am too white for my own good", 
    "I love the way you paste those stickers.", 
    "You probably were not just thinking about Wichita" 
    // Add more sentences if you want to 
]; 

var index = Math.floor(Math.random() * sentences.length); 
alert(sentences[index]); // This will be your random value 

CodePen example