7

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) );
0

1 Answer 1

28

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.

Sign up to request clarification or add additional context in comments.

1 Comment

In my opinion the question linked as duplicate is only similar in some way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.