2017-04-18 115 views
-2

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

任何一个帮助我得到正确的方法来做到这一点?

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

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

回答

2

的代码应该是:

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.

+0

只是一个指针。这是一个不好的做法来回答重复。 – Rajesh