0

I'm kind of new with json-server + faker-js (and generally with javascript). I know the expected basics regarding randomness + seeds so I don't know what's happening in this piece of code below (some expected json-server index.js file), which gives me the same random numbers sequence no matter what:

const { faker } = require('@faker-js/faker');
 
module.exports = () => {

const data = {
    maps: [],
}


faker.seed( new Date() / 1 );

// MAPS

let n_maps = faker.random.numeric(1);

for(let i = 1; i <= n_maps; i++) {

    let mapa = {};

    mapa.id = faker.random.numeric();
    mapa.color = "#"+faker.random.alpha(6);

    data.maps.push(mapa);
}

// MAPS END
 
return data;
}

The above code happily runs under some localhost web server I created with json-server, and it yields some json data like this:

[

{
    "id": "1",
    "color": "#tyhpkc"
},
{
    "id": "3",
    "color": "#twcftk"
},
{
    "id": "2",
    "color": "#natzru"
}

]

The problem is that every RELOAD from the web browser makes the data on screen being the same among "calls". Only when I kill the server and then restart it, the very next web call will get different random numbers... But then again, no new changes of this new data in subsequent calls (web reloads)...

How should I address this situation? (I want true randomness on every web call from the browser, not only once and not only when I restart the server).

Is it possible to do it? Am I missing some important concept within the setup I've created?

Thanks.

Isaac
  • 1,794
  • 4
  • 21
  • 41

0 Answers0