随机函数和持久性在Golang程序中的应用

huangapple go评论124阅读模式
英文:

Random function and persistence in Golang Program

问题

我的程序如下:

我想要做的是看看是否有一种方法可以创建一个随机函数或使用内置函数,该函数能够知道从随机选择中选择的食物,并且在接下来的一周内不再使用它?我目前有从1到6的食物,但我希望确保同样的食物,例如2,在一周内不会连续选择两次。另外,我希望程序能够可能地记录上次选择的项目,以便至少一周内不会再次选择它。我能够通过一个简单的文本文件来实现这个吗?

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "time"
  6. )
  7. type Recipe struct { //Struct for recipe information
  8. name string
  9. prepTime int
  10. cookTime int
  11. Ingredients []string //this is now a slice that will accept multiple elements
  12. ID int
  13. Yield int
  14. }
  15. func main() {
  16. var recipe1 Recipe //Declare recipe1 of Type Recipe
  17. var recipe2 Recipe
  18. var recipe3 Recipe
  19. /* recipe1 specifications */
  20. recipe1.name = "BBQ Pulled Chicken"
  21. recipe1.prepTime = 25
  22. recipe1.cookTime = 5
  23. recipe1.Ingredients = append(
  24. recipe1.Ingredients,
  25. "1 8-ounce can reduced-sodium tomato sauce",
  26. )
  27. recipe1.Ingredients = append(
  28. recipe1.Ingredients,
  29. "1/2 medium onion (grated),",
  30. )
  31. recipe1.ID = 1
  32. recipe1.Yield = 8
  33. /* Recipe 2 specifications */
  34. recipe2.name = "Steak Tacos with Pineapple"
  35. recipe2.prepTime = 45
  36. recipe2.cookTime = 45
  37. recipe2.Ingredients = append(
  38. recipe2.Ingredients,
  39. "3 tablespoons soy sauce,",
  40. )
  41. recipe2.Ingredients = append(
  42. recipe2.Ingredients,
  43. "1 tablespoon finely grated garlic,",
  44. )
  45. recipe2.Ingredients = append(
  46. recipe2.Ingredients,
  47. "1 tablespoon finely grated peeled fresh ginger,",
  48. )
  49. recipe2.Ingredients = append(
  50. recipe2.Ingredients,
  51. "1 1/2 pounds skirt steak, cut into 5-inch lengths,",
  52. )
  53. recipe2.Ingredients = append(
  54. recipe2.Ingredients,
  55. "Salt",
  56. )
  57. recipe2.Ingredients = append(
  58. recipe2.Ingredients,
  59. "Pepper",
  60. )
  61. recipe2.ID = 2
  62. recipe2.Yield = 4
  63. recipe3.name = "Simple Lemon Herb Chicken"
  64. recipe3.prepTime = 10
  65. recipe3.cookTime = 15
  66. recipe3.Ingredients = append(
  67. recipe3.Ingredients,
  68. "2 skinless boneless chicken breast halves,",
  69. )
  70. recipe3.Ingredients = append(
  71. recipe3.Ingredients,
  72. "1 Lemon,",
  73. )
  74. recipe3.Ingredients = append(
  75. recipe3.Ingredients,
  76. "Salt and Pepper to taste,",
  77. )
  78. recipe3.Ingredients = append(
  79. recipe3.Ingredients,
  80. "1 tablespoon olive oil,",
  81. )
  82. recipe3.Ingredients = append(
  83. recipe3.Ingredients,
  84. "2 sprigs fresh parsley (for garnish),",
  85. )
  86. recipe3.Ingredients = append(
  87. recipe3.Ingredients,
  88. "1 pinch dried oregano,",
  89. )
  90. recipe3.ID = 3
  91. recipe3.Yield = 2
  92. //call to printRecipe function below
  93. printRecipe(recipe1)
  94. totalTime(recipe1)
  95. printRecipe(recipe2)
  96. totalTime(recipe2)
  97. printRecipe(recipe3)
  98. totalTime(recipe3)
  99. //choose random number for recipe
  100. rand.Seed(time.Now().UTC().UnixNano())
  101. myrand := random(1, 6)
  102. fmt.Println(myrand)
  103. //logic for recipe to choose
  104. if myrand == 1 {
  105. fmt.Println(1)
  106. printRecipeOfTheDay(recipe1)
  107. } else if myrand == 2 {
  108. fmt.Println(2)
  109. printRecipeOfTheDay(recipe2)
  110. } else if myrand == 3 {
  111. fmt.Println(3)
  112. printRecipeOfTheDay(recipe3)
  113. } else if myrand == 4 {
  114. fmt.Println(4)
  115. }
  116. }
  117. //function to print Recipe
  118. func printRecipe(recipe Recipe) {
  119. fmt.Printf("Recipe Name : %s\n", recipe.name)
  120. fmt.Printf("Prep Time : %d\n", recipe.prepTime)
  121. fmt.Printf("Cook Time : %d\n", recipe.cookTime)
  122. fmt.Printf("Ingredients : %s\n", recipe.Ingredients)
  123. fmt.Printf("Recipe ID : %d\n", recipe.ID)
  124. }
  125. //random number function
  126. func random(min, max int) int {
  127. return rand.Intn(max-min) + min
  128. }
  129. //function to print the winner for recipe of the day to use
  130. //for either lunch or dinner
  131. func printRecipeOfTheDay(recipe Recipe) {
  132. fmt.Printf("The recipe of the day is : %s\n", recipe.name)
  133. }
  134. //Returns total time by addings cookTime and prepTime
  135. func totalTime(recipe Recipe) {
  136. fmt.Printf("The total time for this recipe is %d\n", recipe.cookTime+recipe.prepTime)
  137. }

请注意,这只是一个程序示例,它展示了如何创建一个包含食谱信息的结构体,并使用随机函数选择一个食谱。要实现每周不重复选择相同食物的功能,您可以使用一个简单的文本文件来记录上次选择的食物,并在选择之前检查该文件以确保不会选择相同的食物。

英文:

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?

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "time"
  6. )
  7. type Recipe struct { //Struct for recipe information
  8. name string
  9. prepTime int
  10. cookTime int
  11. Ingredients []string //this is now a slice that will accept multiple elements
  12. ID int
  13. Yield int
  14. }
  15. func main() {
  16. var recipe1 Recipe //Declare recipe1 of Type Recipe
  17. var recipe2 Recipe
  18. var recipe3 Recipe
  19. /* recipe1 specifications */
  20. recipe1.name = "BBQ Pulled Chicken"
  21. recipe1.prepTime = 25
  22. recipe1.cookTime = 5
  23. recipe1.Ingredients = append(
  24. recipe1.Ingredients,
  25. "1 8-ounce can reduced-sodium tomato sauce",
  26. )
  27. recipe1.Ingredients = append(
  28. recipe1.Ingredients,
  29. "1/2 medium onion (grated),",
  30. )
  31. recipe1.ID = 1
  32. recipe1.Yield = 8
  33. /* Recipe 2 specifications */
  34. recipe2.name = "Steak Tacos with Pineapple"
  35. recipe2.prepTime = 45
  36. recipe2.cookTime = 45
  37. recipe2.Ingredients = append(
  38. recipe2.Ingredients,
  39. "3 tablespoons soy sauce,",
  40. )
  41. recipe2.Ingredients = append(
  42. recipe2.Ingredients,
  43. "1 tablespoon finely grated garlic,",
  44. )
  45. recipe2.Ingredients = append(
  46. recipe2.Ingredients,
  47. "1 tablespoon finely grated peeled fresh ginger,",
  48. )
  49. recipe2.Ingredients = append(
  50. recipe2.Ingredients,
  51. "1 1/2 pounds skirt steak, cut into 5-inch lengths,",
  52. )
  53. recipe2.Ingredients = append(
  54. recipe2.Ingredients,
  55. "Salt",
  56. )
  57. recipe2.Ingredients = append(
  58. recipe2.Ingredients,
  59. "Pepper",
  60. )
  61. recipe2.ID = 2
  62. recipe2.Yield = 4
  63. recipe3.name = "Simple Lemon Herb Chicken"
  64. recipe3.prepTime = 10
  65. recipe3.cookTime = 15
  66. recipe3.Ingredients = append(
  67. recipe3.Ingredients,
  68. "2 skinless boneless chicken breast halves,",
  69. )
  70. recipe3.Ingredients = append(
  71. recipe3.Ingredients,
  72. "1 Lemon,",
  73. )
  74. recipe3.Ingredients = append(
  75. recipe3.Ingredients,
  76. "Salt and Pepper to taste,",
  77. )
  78. recipe3.Ingredients = append(
  79. recipe3.Ingredients,
  80. "1 tablespoon olive oil,",
  81. )
  82. recipe3.Ingredients = append(
  83. recipe3.Ingredients,
  84. "2 sprigs fresh parsley (for garnish),",
  85. )
  86. recipe3.Ingredients = append(
  87. recipe3.Ingredients,
  88. "1 pinch dried oregano,",
  89. )
  90. recipe3.ID = 3
  91. recipe3.Yield = 2
  92. //call to printRecipe function below
  93. printRecipe(recipe1)
  94. totalTime(recipe1)
  95. printRecipe(recipe2)
  96. totalTime(recipe2)
  97. printRecipe(recipe3)
  98. totalTime(recipe3)
  99. //choose random number for recipe
  100. rand.Seed(time.Now().UTC().UnixNano())
  101. myrand := random(1, 6)
  102. fmt.Println(myrand)
  103. //logic for recipe to choose
  104. if myrand == 1 {
  105. fmt.Println(1)
  106. printRecipeOfTheDay(recipe1)
  107. } else if myrand == 2 {
  108. fmt.Println(2)
  109. printRecipeOfTheDay(recipe2)
  110. } else if myrand == 3 {
  111. fmt.Println(3)
  112. printRecipeOfTheDay(recipe3)
  113. } else if myrand == 4 {
  114. fmt.Println(4)
  115. }
  116. }
  117. //function to print Recipe
  118. func printRecipe(recipe Recipe) {
  119. fmt.Printf("Recipe Name : %s\n", recipe.name)
  120. fmt.Printf("Prep Time : %d\n", recipe.prepTime)
  121. fmt.Printf("Cook Time : %d\n", recipe.cookTime)
  122. fmt.Printf("Ingredients : %s\n", recipe.Ingredients)
  123. fmt.Printf("Recipe ID : %d\n", recipe.ID)
  124. }
  125. //random number function
  126. func random(min, max int) int {
  127. return rand.Intn(max-min) + min
  128. }
  129. //function to print the winner for recipe of the day to use
  130. //for either lunch or dinner
  131. func printRecipeOfTheDay(recipe Recipe) {
  132. fmt.Printf("The recipe of the day is : %s\n", recipe.name)
  133. }
  134. //Returns total time by addings cookTime and prepTime
  135. func totalTime(recipe Recipe) {
  136. fmt.Printf("The total time for this recipe is %d\n", recipe.cookTime+recipe.prepTime)
  137. }

答案1

得分: 2

根据@Volker的说法,Perm可能是你想要使用的。这里有一个示例,可以为你生成伪随机列表。你可以将其保存到一个以json编码的文件中。然后,如果你有7个食谱,你可以使用time.Weekday来从切片中获取一个食谱编号,使用星期几作为切片的键。一旦到达某个预定的日期,只需重新生成切片并保存。

  1. package main
  2. import "fmt"
  3. import "math/rand"
  4. import "time"
  5. func main() {
  6. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  7. i := r.Perm(6)
  8. fmt.Printf("%v\n", i)
  9. }
英文:

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.

  1. package main
  2. import "fmt"
  3. import "math/rand"
  4. import "time"
  5. func main() {
  6. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  7. i := r.Perm(6)
  8. fmt.Printf("%v\n", i)
  9. }

huangapple
  • 本文由 发表于 2016年2月10日 04:28:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/35301543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定