我想要做的是看看是否有一種方法可以創(chuàng)建一個(gè)隨機(jī)函數(shù)或使用一個(gè)內(nèi)置函數(shù),該函數(shù)能夠使用知道該程序中的哪些食物是從隨機(jī)選擇中選擇的,并且不再用于另一個(gè)星期?我目前有 1-6 種食物,但我想確保一周內(nèi)不會(huì)連續(xù)兩次選擇相同的食物,例如 2。此外,我希望程序能夠?qū)懴伦詈筮x擇的項(xiàng)目,這樣它至少一周內(nèi)不會(huì)再次選擇它。我能用一個(gè)可以讀取的簡(jiǎn)單文本文件來(lái)完成這個(gè)嗎?package mainimport ( "fmt" "math/rand" "time")type Recipe struct { //Struct for recipe information name string prepTime int cookTime int Ingredients []string //this is now a slice that will accept multiple elements ID int Yield int}func main() { var recipe1 Recipe //Declare recipe1 of Type Recipe var recipe2 Recipe var recipe3 Recipe /* recipe1 specifications */ recipe1.name = "BBQ Pulled Chicken" recipe1.prepTime = 25 recipe1.cookTime = 5 recipe1.Ingredients = append( recipe1.Ingredients, "1 8-ounce can reduced-sodium tomato sauce", ) recipe1.Ingredients = append( recipe1.Ingredients, "1/2 medium onion (grated),", ) recipe1.ID = 1 recipe1.Yield = 8 /* Recipe 2 specifications */ recipe2.name = "Steak Tacos with Pineapple" recipe2.prepTime = 45 recipe2.cookTime = 45 recipe2.Ingredients = append( recipe2.Ingredients, "3 tablespoons soy sauce,", ) recipe2.Ingredients = append( recipe2.Ingredients, "1 tablespoon finely grated garlic,", ) recipe2.Ingredients = append( recipe2.Ingredients, "1 tablespoon finely grated peeled fresh ginger,", ) recipe2.Ingredients = append( recipe2.Ingredients, "1 1/2 pounds skirt steak, cut into 5-inch lengths,", ) recipe2.Ingredients = append( recipe2.Ingredients, "Salt", ) recipe2.Ingredients = append( recipe2.Ingredients, "Pepper", )
1 回答

弒天下
TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊
Perm 可能是您想要使用的。這是一個(gè)將為您生成偽隨機(jī)列表的示例。您可以將其保存到 json 編碼的文件中。然后,如果您有 7 個(gè)食譜,您可以使用 time.Weekday 從切片中獲取食譜編號(hào),使用星期幾作為切片的關(guān)鍵。一旦您達(dá)到某個(gè)預(yù)定日期,只需重新生成切片并保存。
package main
import "fmt"
import "math/rand"
import "time"
func main() {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
i := r.Perm(6)
fmt.Printf("%v\n", i)
}
- 1 回答
- 0 關(guān)注
- 209 瀏覽
添加回答
舉報(bào)
0/150
提交
取消