Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript随机加载消息生成器问题_Javascript - Fatal编程技术网

JavaScript随机加载消息生成器问题

JavaScript随机加载消息生成器问题,javascript,Javascript,我对Javascript了解不多,而且我编写的这个函数似乎工作不正常。我不知道哪里出了问题,只是有时候它不能正常工作 Any advice on how to improve it? function GetRandomLoadingMessage() { var d = new Date(); var weekday = d.getDay(); var month = d.getMonth(); var seed = weekday + "" + year

我对Javascript了解不多,而且我编写的这个函数似乎工作不正常。我不知道哪里出了问题,只是有时候它不能正常工作

Any advice on how to improve it?

function GetRandomLoadingMessage() 
{
    var d = new Date();
    var weekday = d.getDay();
    var month = d.getMonth();

    var seed = weekday + "" + year;

    var choice = Math.round(Math.random(seed) * 2);

    if (choice == 1) {
        var lines = new Array(
        "Pay no attention to the man behind the curtain",
        "Follow the white rabbit",
        "The satellite is moving into position",
        "You're not in Kansas anymore",
        "Reticulating Splines",
        "The gods conemplate your fate",
        "It's not you, it's me",
        "So, do you come here often?",
        "Counting backwards from infinity",
        "Rolling for initiative");
    }

    if (choice == 2) {
        var lines = new Array(
        "E.T. phone home",
        "May the force be with you",
        "Here's looking at you, kid",
        "I'm gonna make him an offer he can't refuse",
        "Bond. James Bond.",
        "You're gonna need a bigger boat",
        "I'll be back",
        "Soylent Green is people!",
        "Open the pod bay doors, HAL",
        "Shaken, not stirred");

    }

    if (choice == 3) {
        var lines = new Array(
        "Parallel processors running perpendicular today",
        "Interference from lunar radiation",
        "Positron routers depleted",
        "Borg nanites infesting the server",
        "Astropneumatic oscillations inducing delays",
        "Increased sunspot activity",
        "Packets were eaten by the terminator",
        "Network packets travelling uphill",
        "Trojan horse ran out of hay",
        "Change in the earth's rotational speed");

    }

        return lines[Math.round(Math.random()*(lines.length-1))];

}
正如您可能已经猜到的,这个想法是每天随机显示来自某个特定“部分”的消息。。。因此使用种子。虽然我应该每年换一个月什么的


编辑:好的!谢谢你提供的信息。但我真的需要弄清楚如何拥有一颗种子,就像在C#中一样。有办法吗?每天我都希望它选择一个列表,并从列表中给出响应。通过从天/月的组合中进行种子设定,我可以始终为给定的一天使用相同的列表。

首先,您可以通过以下方式对随机列表进行种子设定:

var n = new Date().getTime();
然后,使用switch语句而不是三个单独的if(或嵌套的if/else):

你也可以考虑只需选择一个大数组,如果不需要这样分组,只需在0和列表1的长度之间挑选一个随机数,然后返回数组中的索引:

var lines = [...];
function random_line() {
    var r = Math.random();
    var idx = Math.floor( r * (lines.length - 1) );
    return lines[idx];
}

首先,您可以使用以下方法对随机数据进行种子设定:

var n = new Date().getTime();
然后,使用switch语句而不是三个单独的if(或嵌套的if/else):

你也可以考虑只需选择一个大数组,如果不需要这样分组,只需在0和列表1的长度之间挑选一个随机数,然后返回数组中的索引:

var lines = [...];
function random_line() {
    var r = Math.random();
    var idx = Math.floor( r * (lines.length - 1) );
    return lines[idx];
}

您尚未声明变量
year
,因此种子值将与您期望的不完全一样。当它被转换为用于
random
函数时,它很可能只使用工作日值。您可能打算改用
month
变量:

var seed = weekday + "" + month;
你的随机计算不正确。您将获得第一个和最后一个项目发生次数的一半。您应该使用
floor
方法而不是
round
,并乘以一个更大的值:

var choice = Math.floor(Math.random(seed) * 3);

编辑:
当然,选择的值从0到2不等,而不是从1到3不等

此外,正如Jon Benedicto在他的回答中指出的,随机方法不带任何参数,因此种子值将被忽略。如果你想每天得到一个随机值,你必须自己做伪随机计算。例如:

var now = new Date();
var choice = (now.getDay() * 251 + now.getDate()) % 3;

您尚未声明变量
year
,因此种子值将与您期望的不完全一样。当它被转换为用于
random
函数时,它很可能只使用工作日值。您可能打算改用
month
变量:

var seed = weekday + "" + month;
你的随机计算不正确。您将获得第一个和最后一个项目发生次数的一半。您应该使用
floor
方法而不是
round
,并乘以一个更大的值:

var choice = Math.floor(Math.random(seed) * 3);

编辑:
当然,选择的值从0到2不等,而不是从1到3不等

此外,正如Jon Benedicto在他的回答中指出的,随机方法不带任何参数,因此种子值将被忽略。如果你想每天得到一个随机值,你必须自己做伪随机计算。例如:

var now = new Date();
var choice = (now.getDay() * 251 + now.getDate()) % 3;

使用
else if
并将
if(选项==3)
替换为
else
以确保始终分配

在块外声明
(向上移动
var行;
并在代码块中分配
)。否则,当
if
中的代码块终止时,将删除局部变量


最后,获取Firefox并安装,并查看错误控制台中是否存在任何进一步的错误。

使用
else if
并将
if(choice==3)
替换为
else
,以确保始终分配

在块外声明
(向上移动
var行;
并在代码块中分配
)。否则,当
if
中的代码块终止时,将删除局部变量


最后,获取Firefox并安装,并查看错误控制台中是否有任何进一步的错误。

根据,
Math.random()
不接受任何参数。是吗?

根据,
Math.random()
不接受任何参数。是吗?

该代码有几个问题

  • random()不接受任何参数,因此可以删除该种子代码

  • 选项将用0到2之间的数字初始化,但您希望它是1到3。简单修复,在指定给选项之前,向值添加1:

    var choice=Math.round(Math.random()*2)+1

  • 不要使用Math.round(Math.random()*(n-1))获取随机范围,请使用Math.floor(Math.random()*n)。因此,您的2个随机数生成器变成:

    var choice=Math.floor(Math.random()*3)+1

    返回行[Math.floor(Math.random()*lines.length)]


  • 就我个人而言,我不会为这两个随机数而烦恼,我会将所有行合并到一个数组中,并使用一个随机数。

    该代码有几个问题

  • random()不接受任何参数,因此可以删除该种子代码

  • 选项将用0到2之间的数字初始化,但您希望它是1到3。简单修复,在指定给选项之前,向值添加1:

    var choice=Math.round(Math.random()*2)+1

  • 不要使用Math.round(Math.random()*(n-1))获取随机范围,请使用Math.floor(Math.random()*n)。因此,您的2个随机数生成器变成:

    var choice=Math.floor(Math.random()*3)+1

    返回行[Math.floor(Math.random()*lines.length)]


  • 就我个人而言,我不会为这两个随机数而烦恼,我会将所有的行合并到一个数组中,然后使用一个随机数。

    有时无法正常工作。。。怎样?唱诗班