dongxingdu9594 2016-02-09 20:28
浏览 49
已采纳

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

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
", recipe.name)
    fmt.Printf("Prep Time : %d
", recipe.prepTime)
    fmt.Printf("Cook Time : %d
", recipe.cookTime)
    fmt.Printf("Ingredients : %s
", recipe.Ingredients)
    fmt.Printf("Recipe ID : %d
", 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
", recipe.name)
}

//Returns total time by addings cookTime and prepTime
func totalTime(recipe Recipe) {
    fmt.Printf("The total time for this recipe is %d
", recipe.cookTime+recipe.prepTime)
}
  • 写回答

1条回答 默认 最新

  • duanhuan1147 2016-02-10 04:53
    关注

    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
    ", i)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目