1

所以我所拥有的是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

4 回答 4

2

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

演示

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

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

document.write( getRandomName() );
于 2013-02-19T16:31:55.107 回答
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]);

现在,您可以随意添加到阵列中。

于 2013-02-19T16:31:39.027 回答
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
于 2013-02-19T16:31:28.447 回答
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 示例

于 2013-02-19T16:32:08.947 回答