-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
149 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Seeding a RNG using network latency</title> | ||
<script type="text/javascript" src="LatencySource.js"></script> | ||
<script type="text/javascript"> | ||
async function seedRNG() { | ||
document.getElementById('seed').value = await getNetworkLatencySeed(); | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<button onclick="seedRNG()">Seed</button> | ||
<input type="text" name="seed" id="seed" value="none" disabled="disabled" /> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
async function getNetworkLatencySeed() { | ||
return await _getNetworkLatencySeed( | ||
[ | ||
'https://google.com', | ||
'https://bing.com', | ||
'https://yahoo.com', | ||
'https://duckduckgo.com', | ||
'https://ecosia.org', | ||
], | ||
10 | ||
); | ||
} | ||
|
||
async function _getNetworkLatencySeed(hosts, count) { | ||
let seed = 0; | ||
|
||
const getLatency = (host) => | ||
new Promise((resolve) => { | ||
const start = Date.now(); | ||
fetch(host, { mode: 'no-cors' }) | ||
.then(() => resolve(Date.now() - start)) | ||
.catch(() => resolve(Date.now() - start)); // resolve even if the fetch fails | ||
}); | ||
|
||
const combineSeed = (seed, latency) => (seed * 31 + latency) % Number.MAX_SAFE_INTEGER; | ||
|
||
for (let i = 0; i < count; i++) { | ||
const latency = await getLatency(hosts[i % hosts.length]); | ||
seed = combineSeed(seed, latency); | ||
} | ||
|
||
return seed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters