Algorithm Golang程序中的随机函数与持久性

Algorithm Golang程序中的随机函数与持久性,algorithm,go,persistence,Algorithm,Go,Persistence,我的计划如下: 我想做的是看看是否有一种方法可以创建一个随机函数,或者使用一个内置函数,能够使用这个程序中的哪些食物是从随机选择中选择的,并且在接下来的一周内不再使用?我目前的食物设置为1-6,但我想确保一周内不会连续两次选择相同的食物(例如2)。此外,我希望该计划能够有可能写什么,最后一个项目是选定的,所以它不会再选择它至少一个星期。我能用一个简单的文本文件来完成这个任务吗 package main import ( "fmt" "math/rand" "time"

我的计划如下:

我想做的是看看是否有一种方法可以创建一个随机函数,或者使用一个内置函数,能够使用这个程序中的哪些食物是从随机选择中选择的,并且在接下来的一周内不再使用?我目前的食物设置为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)
}

正如@Volker所说,烫发很可能是您想要使用的。下面的示例将为您生成伪随机列表。您可以将其保存到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)
}

你正在寻找一个排列:数学/随机排列中的排列你知道我可能遵循的一个例子吗?有趣的是,我注意到输出为[5 2 3 0 1 4]我如何能够将其中一个数字读入一个变量中单独处理?例如,我想把5读入一个我称之为monday的变量,它是一个int片,所以你可以引用片中包含的任何int,比如i[0]或i[5],直到i[len(i)-1]。在这之后,你会得到一个越界的错误。哇,这是完美的,谢谢肖恩!正是我要找的!