Python Forum
Make Random Choices the same [SOLVED]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Make Random Choices the same [SOLVED]
#1
Hello everybody,

I wanted to set up a list of strings and print one randomly.
This was really easy to set up:
#Random Variables
myList = ["Example 1", "Example 1", "Example 1", "Example 1"]
random = random.choice(myList)

print(random)
Now since this script (it's of course only an example, my actual script is more complex but I'm only focussing on the important part here) runs twice a day, I want the random choice to be the same if the day is still the same.

For example, if I run this script five times a day I always want it to be example 4 and on another day it should pick another random which stays for this day.
Is it possible to do this and share it with another script or do I need to create some kind of config which stores the daily random-pick ?
Reply
#2
(May-16-2022, 09:17 AM)AlphaInc Wrote: For example, if I run this script five times a day I always want it to be example 4 and on another day it should pick another random which stays for this day.
Is it possible to do this and share it with another script or do I need to create some kind of config which stores the daily random-pick ?
Can store the random value and day in eg dictionary and save to json.
The do a check if same day and then print same value all day if True.
New day will store a new value and print same value until next day.
To give a example.
# rand_pick.py
import random
import pendulum
import json

def store(save_day):
    with open('rand_today.json', 'w') as f:
        json.dump(save_day, f)

myList = ["Example 1", "Example 2", "Example 3", "Example 4"]
pick_val = random.choice(myList)
today = pendulum.today().day
#today = 18
save_day = {}
save_day[pick_val] = today

if __name__ == '__main__':
    store(save_day)
# day_output.py
import rand_pick
import json
import pendulum

with open('rand_today.json') as f:
    pick = json.load(f)

today = pendulum.today().day
#today = 18
if today in pick.values():
    print(''.join(pick.keys()))
else:
    # No match so a new day save new random vaule
    rand_pick.store(rand_pick.save_day)
Output:
Example 4 Example 4 Example 4
Reply
#3
(May-16-2022, 12:23 PM)snippsat Wrote:
(May-16-2022, 09:17 AM)AlphaInc Wrote: For example, if I run this script five times a day I always want it to be example 4 and on another day it should pick another random which stays for this day.
Is it possible to do this and share it with another script or do I need to create some kind of config which stores the daily random-pick ?
Can store the random value and day in eg dictionary and save to json.
The do a check if same day and then print same value all day if True.
New day will store a new value and print same value until next day.
To give a example.
# rand_pick.py
import random
import pendulum
import json

def store(save_day):
    with open('rand_today.json', 'w') as f:
        json.dump(save_day, f)

myList = ["Example 1", "Example 2", "Example 3", "Example 4"]
pick_val = random.choice(myList)
today = pendulum.today().day
#today = 18
save_day = {}
save_day[pick_val] = today

if __name__ == '__main__':
    store(save_day)
# day_output.py
import rand_pick
import json
import pendulum

with open('rand_today.json') as f:
    pick = json.load(f)

today = pendulum.today().day
#today = 18
if today in pick.values():
    print(''.join(pick.keys()))
else:
    # No match so a new day save new random vaule
    rand_pick.store(rand_pick.save_day)
Output:
Example 4 Example 4 Example 4

Thank you :)
Last problem, when I run the script I get this error, any idea?:

Traceback (most recent call last):
  File "helios_daySavingVar.py", line 4, in <module>
    import rand_pick
ModuleNotFoundError: No module named 'rand_pick'
Reply
#4
Both file is same folder,and you run day_output.py(which import rand_pick.py).
Also install pendulum that i use.
pip install pendulum
Example.
div_code\
  |-- rand_pick.py
  |-- day_output.py
Output:
G:\div_code λ python day_output.py Example 2 G:\div_code λ python day_output.py Example 2 G:\div_code λ python day_output.py Example 2
Reply
#5
(May-17-2022, 01:27 PM)snippsat Wrote: Both file is same folder,and you run day_output.py(which import rand_pick.py).
Also install pendulum that i use.
pip install pendulum
Example.
div_code\
  |-- rand_pick.py
  |-- day_output.py
Output:
G:\div_code λ python day_output.py Example 2 G:\div_code λ python day_output.py Example 2 G:\div_code λ python day_output.py Example 2

Yes, both files are in the same directory and pendulum is installed but I still get this error Confused
Reply
#6
Try to exactly what show,also same name on the files.
Here you use a file helios_daySavingVar.py
Traceback (most recent call last):
  File "helios_daySavingVar.py", line 4, in <module>
    import rand_pick
ModuleNotFoundError: No module named 'rand_pick'
Only when it work,then try to implement into your own code.
Reply
#7
(May-17-2022, 01:41 PM)snippsat Wrote: Try to exactly what show,also same name on the files.
Here you use a file helios_daySavingVar.py
Traceback (most recent call last):
  File "helios_daySavingVar.py", line 4, in <module>
    import rand_pick
ModuleNotFoundError: No module named 'rand_pick'
Only when it work,then try to implement into your own code.

Ah shit... Sorry, I oversaw that ^^ Thank you, now it works :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create Choices from .ods file columns cspower 3 608 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  random.choices and random.sample azizrasul 5 2,263 Sep-16-2022, 08:10 PM
Last Post: deanhystad
  Sending random images via smtplib [SOLVED] AlphaInc 0 1,693 Oct-19-2021, 10:10 AM
Last Post: AlphaInc
  optimize choices in multiple if ---: statements Pedroski55 2 2,898 Dec-25-2018, 05:06 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020