gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:08:00 25 4
gpt4 key购买 nike

所以我有的是http://garrettstelly.com在启动时吐出二十个术语之一。代码非常繁重,我对此没有意见。

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

How can I make a script with an endless amount of possibilities to be added one at a time?

这是 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.');}

最佳答案

将可能性存储在一个数组中,并得到数组的随机元素:

Demo

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

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

document.write( getRandomName() );

关于javascript - (JavaScript) 随机文本输出 - 建议?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14962344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com