使用Javascript创建虚假IP地址

时间:2017-04-18 05:48:29

标签: javascript random

我正在尝试使用以下javascript随机方法创建假IP address。但不行。

任何人都帮我找到正确的方法吗?

这是我的尝试:我看起来像("192.168.10.2"

 console.log(  Math.random(256) + "." + Math.random(256) + "." + Math.random(256) + "." + Math.random(256) );

1 个答案:

答案 0 :(得分:9)

代码应为:

var ip = (Math.floor(Math.random() * 255) + 1)+"."+(Math.floor(Math.random() * 255) + 0)+"."+(Math.floor(Math.random() * 255) + 0)+"."+(Math.floor(Math.random() * 255) + 0);
console.log(ip)

代码(Math.floor(Math.random() * 255) + 1)表示生成1到255之间的随机数。