我對 JavaScript 比較陌生,我正在尋找創建類似 Wordle 克隆的東西。像 Wordle 一樣,我想每天為所有用戶生成一個新詞。我見過使用亂數生成器將數字存盤在本地存盤中的解決方案。但是,這不是意味著每個訪問網站的用戶都有不同的號碼嗎?有沒有一種簡單的方法可以為網站上的所有用戶生成相同的號碼?
uj5u.com熱心網友回復:
您可以制作自己的偽隨機生成器,它們實際上并不是隨機的,而是根據種子(即日期)生成數字
// Get the day of the month with Date object
const day = new Date().getDate();
// And the month to prevent repeats
const month = new Date().getMonth();
然后你可以創建你的函式。
為了讓它看起來更加隨機,您可以獲取中間數字并使用它們。
function random(){
// Crazy math stuff
let num = Math.round((day 4) / month * 39163).toString();
// To convert it back to a number, use the operator before parentheses
// Don’t forget to use % on the max value, I just put 31 as a placeholder
return (num[2] num[3]) % 31;
}
可以根據您的需要進行更改^
這對于每個用戶一整天都是相同的,因為它是偽隨機的并且基于種子
uj5u.com熱心網友回復:
LocalStorage 是瀏覽器的一個特性,它只存在于客戶端。
您需要將資料保存在某個地方、資料庫(MySQL、postgres 等)或檔案中
最簡單的方法是在一個檔案中,你可以搜索谷歌“將資料保存到檔案 node.js”
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/486003.html
標籤:javascript 节点.js
