Ben_Sven_Ten
Ben_Sven_Ten

Reputation: 579

generate random number where whole number is the same but the decimals are randomized javascript

I am creating a script to make a very large object to test some filtering functionality in my app.

Each object has a latitude and a longitude which should be a random number. The number needs to be a float and be randomized within certain parameters

latitude: 43.82965739999999
longitude: -79.4702567

How can I generate a random number where the floating points are randomized?

random output 1

latitude: 43.53829
longitude: -79.5234262

random output 2

latitude: 43.908234294234
longitude: -79.19092342

Upvotes: 2

Views: 124

Answers (1)

Nick Vu
Nick Vu

Reputation: 15520

You just simply use Math.random() in your code, this function always returns a number below 1.

The implementation can be like this

const latitude = 43 + Math.random();
const longitude = -79 - Math.random();

Upvotes: 6

Related Questions