(JavaScript)随机文本输出 - 建议?

时间:2013-02-19 16:24:08

标签: javascript random

所以我所拥有的是http://garrettstelly.com,它在启动时吐出二十个术语中的一个。代码很重,我很好。

我打算让一个网站随机吐出一个名字,问题是我可以拥有无​​穷无尽的名字。对于我当前使用的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.');}

4 个答案:

答案 0 :(得分:2)

将可能性存储在数组中并获取数组的随机元素:

Demo

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

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

document.write( getRandomName() );

答案 1 :(得分: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()但乘以数组的长度:

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

然后使用适当的数组索引编写结果,在您的情况下使用document.write(尽管可能有更好的方法):

document.write(arr[roll]);

现在您可以根据需要添加到数组中。

答案 2 :(得分: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

答案 3 :(得分: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