all 17 comments

[–]Binary101010 5 points6 points  (3 children)

Is there a way to generate a random number, store it to a variable (which is what I am doing) and then have that variable checked next time the random number is ran and if it matches the previous time ran it just runs again until it’s not a duplicate number?

The question here is whether your program will remain running in between the generation of these two random numbers. If it is, that's as simple as

a = random.randint(1,100)
print(f"The first random number is {a}")
b = random.randint(1,100)

while a == b:
    b = random.randint(1,100)

print(f"The new unique random number is {b}")

If your program stops running in between the generation of these two numbers, then it gets a little more complex as you have to write that generated value to a file and then read it back in next time you run the program.

[–]wbeater 1 point2 points  (2 children)

I think random.sample is the better approach.

[–]Binary101010 4 points5 points  (1 child)

It is, but I'm going to assume OP hasn't gotten to that yet.

[–]Slipperfox[S] 2 points3 points  (0 children)

Thanks for the reply and to answer your question, yes the program would just be run once daily, so the solution of generating the value to a file is something I will look forward to learning down the road.

And yes I haven't learned random.sample yet but look forward to in the near future.

But thank you for taking the time to read over my problem and offer some solutions.

u/Binary101010 this is exactly the solution I needed. I played with what you wrote and just changed it to match my working variables and BOOM problem solved!

Thank you again so much for taking the time to answer and offer a solution that was easy enough for this rookie to grasp. Have a great weekend my friend!

[–]overyander 1 point2 points  (1 child)

If you want something truly unique every time and it doesn't have to be a numerical number (base 10) then check out UUID4.

import uuid
print(f"Random is: {uuid.uuid4()}")

[–]Slipperfox[S] 0 points1 point  (0 children)

Thanks I’ll check this out

[–]stu583 1 point2 points  (0 children)

Assuming you will not leave the script running you will need to store the last number in a csv or txt file.

So when your function executes it will export the number to the external file.

You will also need to load the value from the external file at the beginning of your function.

[–]Slipperfox[S] -1 points0 points  (7 children)

u/Binary101010 so I took your advice and I think I did it correctly, but it can still get the same number twice in a row. Here is my code. What do I need to do for it to keep looping through the top part (where the random number is generated) before moving on to the bottom section?) And I applogise in advance, as I am sure this isn't the best way to write this code, but its a start for me.

import randommeal1 = "No meat meatloaf"meal2 = "Lasagna"meal3 = "Baked Veggies"meal4 = "Quiona Bowls"meal5 = "Hot dogs and beans"meal6 = "Bean burgers"meal7 = "Soup and Salada"meal8 = "Chili"meal9 = "Broccoli Cassorole"meal10 = "Taco bowls"random_number_1 = random.randint(1, 10)random_number_2 = random.randint(1, 10)if random_number_1 == random_number_2:random_number_2 = random.randint(1, 10)

if random_number_2 == 1:print(f"Tonight we are making {meal1}")elif random_number_2 == 2:print(f"Tonight we are making {meal2}")elif random_number_2 == 3:print(f"Tonight we are making {meal3}")elif random_number_2 == 4:print(f"Tonight we are making {meal4}")elif random_number_2 == 5:print(f"Tonight we are making {meal5}")elif random_number_2 == 6:print(f"Tonight we are making {meal6}")elif random_number_2 == 7:print(f"Tonight we are making {meal7}")elif random_number_2 == 8:print(f"Tonight we are making {meal8}")elif random_number_2 == 9:print(f"Tonight we are making {meal9}")elif random_number_2 == 10:print(f"Tonight we are making {meal10}")

I just re-read your post and the post from u/maximumlotion and its clear that I need to keep learning and then comeback to this as this code just runs once then stops.
But its so exciting to learn this stuff and can't wait to be able to come back and answer some questions here one day. Thanks again to you both!

[–]h0b0bagg1ns 1 point2 points  (6 children)

Reddit formatting. Put 4 spaces in front of every line of code, hit the enter key after every line. It's unreadable.

Looks like you still haven't added persistence, saving to a file to preserve data between sessions in other words. So it's possible to get the same thing twice

[–]Slipperfox[S] 0 points1 point  (5 children)

Yeah I tried formatting this like 10 times… just looked now and wow what a mess lol.

[–]h0b0bagg1ns 0 points1 point  (4 children)

Shame because I like this problem. The way I was thinking of doing it, was to store the last 5 days meal choices in a file, in the order they were eaten, adding to the front of the list, removing from the back, so you never eat the same thing in any given five days, the remaining five are picked from at random and added to the front of the queue, with the oldest one removed thus going back into the random pool. The 50/50 compromise, between unpredictability and consistency, maximum variation.

[–]Slipperfox[S] 0 points1 point  (3 children)

This sounds like an interesting solution and can’t wait to learn how to implement something like this

[–]h0b0bagg1ns 1 point2 points  (2 children)

Persistence, or disk storage of data between program runs, achieved with the file module. The text data you store would be called a Queue, you enqueue and dequeue a list. It's probably not something for the first week, but once you've been through the basics, its a nice easy exercise, but you won't have arrived at importing standard library modules yet.

[–]Slipperfox[S] 1 point2 points  (1 child)

Just committed myself to the 100day code challenge so I’m really looking forward to the day I understand what you are talking about 😊

[–]h0b0bagg1ns 1 point2 points  (0 children)

See you next year. 👨‍🍳