4

I am trying to create fake IP address using the following javascript random method. But not works.

any one help me to get the correct way to do this?

here is my try: I am looking like ( "192.168.10.2")

 console.log(  Math.random(256) + "." + Math.random(256) + "." + Math.random(256) + "." + Math.random(256) );
user2024080
  • 1
  • 14
  • 56
  • 96

1 Answers1

25

The code should be :

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

The code (Math.floor(Math.random() * 255) + 1) means generate a random number between 1 and 255.

Seb
  • 888
  • 12
  • 20
Shakti Phartiyal
  • 6,156
  • 3
  • 25
  • 46