英文:
Random function and persistence in Golang Program
问题
我的程序如下:
我想要做的是看看是否有一种方法可以创建一个随机函数或使用内置函数,该函数能够知道从随机选择中选择的食物,并且在接下来的一周内不再使用它?我目前有从1到6的食物,但我希望确保同样的食物,例如2,在一周内不会连续选择两次。另外,我希望程序能够可能地记录上次选择的项目,以便至少一周内不会再次选择它。我能够通过一个简单的文本文件来实现这个吗?
package main
import (
"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",
)
recipe2.ID = 2
recipe2.Yield = 4
recipe3.name = "Simple Lemon Herb Chicken"
recipe3.prepTime = 10
recipe3.cookTime = 15
recipe3.Ingredients = append(
recipe3.Ingredients,
"2 skinless boneless chicken breast halves,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 Lemon,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"Salt and Pepper to taste,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 tablespoon olive oil,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"2 sprigs fresh parsley (for garnish),",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 pinch dried oregano,",
)
recipe3.ID = 3
recipe3.Yield = 2
//call to printRecipe function below
printRecipe(recipe1)
totalTime(recipe1)
printRecipe(recipe2)
totalTime(recipe2)
printRecipe(recipe3)
totalTime(recipe3)
//choose random number for recipe
rand.Seed(time.Now().UTC().UnixNano())
myrand := random(1, 6)
fmt.Println(myrand)
//logic for recipe to choose
if myrand == 1 {
fmt.Println(1)
printRecipeOfTheDay(recipe1)
} else if myrand == 2 {
fmt.Println(2)
printRecipeOfTheDay(recipe2)
} else if myrand == 3 {
fmt.Println(3)
printRecipeOfTheDay(recipe3)
} else if myrand == 4 {
fmt.Println(4)
}
}
//function to print Recipe
func printRecipe(recipe Recipe) {
fmt.Printf("Recipe Name : %s\n", recipe.name)
fmt.Printf("Prep Time : %d\n", recipe.prepTime)
fmt.Printf("Cook Time : %d\n", recipe.cookTime)
fmt.Printf("Ingredients : %s\n", recipe.Ingredients)
fmt.Printf("Recipe ID : %d\n", recipe.ID)
}
//random number function
func random(min, max int) int {
return rand.Intn(max-min) + min
}
//function to print the winner for recipe of the day to use
//for either lunch or dinner
func printRecipeOfTheDay(recipe Recipe) {
fmt.Printf("The recipe of the day is : %s\n", recipe.name)
}
//Returns total time by addings cookTime and prepTime
func totalTime(recipe Recipe) {
fmt.Printf("The total time for this recipe is %d\n", recipe.cookTime+recipe.prepTime)
}
请注意,这只是一个程序示例,它展示了如何创建一个包含食谱信息的结构体,并使用随机函数选择一个食谱。要实现每周不重复选择相同食物的功能,您可以使用一个简单的文本文件来记录上次选择的食物,并在选择之前检查该文件以确保不会选择相同的食物。
英文:
My program is below:
What I'm trying to do is see if there is a way to create a random function or use a built in function that is able to use know what food in this program was selected from the random selection and have it not used again for another week? I currently have foods set from 1-6 but I want to make sure the same food for example 2 isn't selected twice in a row for a week. Also I want the program to be able to possibly write what the last item that was selected so it won't choose it again for atleast a week. Would i be able to accomplish this with a simple text file that can be read from?
package main
import (
"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",
)
recipe2.ID = 2
recipe2.Yield = 4
recipe3.name = "Simple Lemon Herb Chicken"
recipe3.prepTime = 10
recipe3.cookTime = 15
recipe3.Ingredients = append(
recipe3.Ingredients,
"2 skinless boneless chicken breast halves,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 Lemon,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"Salt and Pepper to taste,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 tablespoon olive oil,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"2 sprigs fresh parsley (for garnish),",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 pinch dried oregano,",
)
recipe3.ID = 3
recipe3.Yield = 2
//call to printRecipe function below
printRecipe(recipe1)
totalTime(recipe1)
printRecipe(recipe2)
totalTime(recipe2)
printRecipe(recipe3)
totalTime(recipe3)
//choose random number for recipe
rand.Seed(time.Now().UTC().UnixNano())
myrand := random(1, 6)
fmt.Println(myrand)
//logic for recipe to choose
if myrand == 1 {
fmt.Println(1)
printRecipeOfTheDay(recipe1)
} else if myrand == 2 {
fmt.Println(2)
printRecipeOfTheDay(recipe2)
} else if myrand == 3 {
fmt.Println(3)
printRecipeOfTheDay(recipe3)
} else if myrand == 4 {
fmt.Println(4)
}
}
//function to print Recipe
func printRecipe(recipe Recipe) {
fmt.Printf("Recipe Name : %s\n", recipe.name)
fmt.Printf("Prep Time : %d\n", recipe.prepTime)
fmt.Printf("Cook Time : %d\n", recipe.cookTime)
fmt.Printf("Ingredients : %s\n", recipe.Ingredients)
fmt.Printf("Recipe ID : %d\n", recipe.ID)
}
//random number function
func random(min, max int) int {
return rand.Intn(max-min) + min
}
//function to print the winner for recipe of the day to use
//for either lunch or dinner
func printRecipeOfTheDay(recipe Recipe) {
fmt.Printf("The recipe of the day is : %s\n", recipe.name)
}
//Returns total time by addings cookTime and prepTime
func totalTime(recipe Recipe) {
fmt.Printf("The total time for this recipe is %d\n", recipe.cookTime+recipe.prepTime)
}
答案1
得分: 2
根据@Volker的说法,Perm可能是你想要使用的。这里有一个示例,可以为你生成伪随机列表。你可以将其保存到一个以json编码的文件中。然后,如果你有7个食谱,你可以使用time.Weekday来从切片中获取一个食谱编号,使用星期几作为切片的键。一旦到达某个预定的日期,只需重新生成切片并保存。
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)
}
英文:
As @Volker said Perm is likely what you want to use. Here's an example that will generate the pseudo random list for you. You could just save that to a file json encoded. Then if you had 7 recipes you could use time.Weekday to grab a recipe number from the slice using the day of the week as key to the slice. Once you hit some pre-determined day just regenerate the slice and save.
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论