-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRandom Fun.py
More file actions
226 lines (157 loc) · 6.63 KB
/
Random Fun.py
File metadata and controls
226 lines (157 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'''Minute Python Program Examples'''
# Python's Random Generator Module
# Created by Joseph C. Richardson, GitHub.com
# Let's have some random fun with Python's Random Generator Module
# But first, let's 'import' the random generator module so we can use it
# and have some fun with it.
# Please note: numpy random generator functions aren't shown here.
# Only those that use Python's built-in Random Generator are shown here.
# HIGHLIGHT AND COPY CODE, THEN PASTE INTO YOUR PREFERABLE PYTHON APP/IDLE
import random
# Let's call a random number with our first random generator example,
# using the 'random.randint()' function.
random_num=random.randint(1,9)
print(random_num)
# Let's try the 'random.shuffle()' function
shuffle_nums_list=[1,2,3,4,5,6,7,8,9]
rand_shuffle=random.shuffle(shuffle_nums_list)
print(shuffle_nums_list[0])
# Let's try using words in our 'random.shuffle()' function and see what
# happens.
shuffle_words_list=[
'Random Value 1',
'Random Value 2',
'Random Value 3',
'Random Value 4',
'Random Value 5']
rand_shuffle=random.shuffle(shuffle_words_list)
print(shuffle_words_list[0])
# Let's try the 'random.choice()' function
random_nums_choice_list=[1,2,3,4,5,6,7,8,9]
random_choice=random.choice(random_nums_choice_list)
print(random_choice)
# Let's try using words in our 'random.choice()' function and see what
# happens.
random_words_choice_list=[
'Random Value 1',
'Random Value 2',
'Random Value 3',
'Random Value 4',
'Random Value 5']
random_choice=random.choice(random_words_choice_list)
print(random_choice)
# Now, let's try the 'random.choices()' function
random_nums_choice_list=[1,2,3,4,5,6,7,8,9]
print(random.choices(random_nums_choice_list,
weights=[100,80,70,60,50,40,30,20,10],k=8)[0])
# Let's try using words in our 'random.choices()' function and see what
# happens.
random_words_choices_list=[
'Random Value 1',
'Random Value 2',
'Random Value 3',
'Random Value 4',
'Random Value 5']
print(random.choices(random_words_choices_list,
weights=[100,80,70,60,50],k=8)[0])
# The beauty of the 'random.choices()' function is that you can increase
# or decrease the odds of how many times a random value will show up
# in the screen output verses the random values, who's odds are much
# lower, due to the 'weights' implementer.
# Other uses of Python's Random Generator are as follows:
random.seed(10)
print(random.random())
random.seed(10)
print(random.getstate())
print(random.getrandbits(4))
print(random.random())
# capture the state:
state=random.getstate()
# print another random number:
print(random.random())
# restore the state:
random.setstate(state)
# and the next random number should be
# the same as when you captured the state:
print(random.random())
'''----------------------------------------------------------------'''
# This conditional while-loop example compares a random number
# against user input data. If the user picks the right number by luck
# alone, the while-loop will break out and the program ends. If the
# user picks the wrong number, the less (<) than or greater (>) than
# 'random_num' variable gets conditioned and the while-loop keeps
# on iterating until the right number is picked, via user input data.
# Note: Python executes/runs programs starting from the top, downward.
# Be very careful on how you place statements. Some statements
# cannot execute right, even if they work. This is simply because of
# the order that Python executes/runs its program statements.
# Note: The 'import random' module must be imported first.
import random
random_num=random.randint(1,10)
while True:
try:
pick_num=int(input('\nWhat number am I thinking of? Hint! It\'s \
between 1 and 10: ').strip())
if pick_num<random_num:
print('\nThe number I\'m thinking of is too low!')
elif pick_num>random_num:
print('\nThe number I\'m thinking of is too high!')
elif pick_num==random_num:
print(f'\nCongratulations! You won. "{random_num}" was the \
number I was thinking of.')
break
except ValueError:
print('\nYou must type integers only please!')
'''----------------------------------------------------------------'''
# This very same program example as above works exactly the same
# way, but with one major difference; the while loop will only iterate three
# times. If the user picks the right number, the while loop breaks. If the
# user doesn't pick the right number after three times, the 'else' statement
# executes and says 'Sorry! You lost.', which ends the program.
# Note: the 'import random' module must be imported first.
import random
random_num=random.randint(1,10)
i=0
while i<3:
try:
pick_num=int(input('\nWhat number am I thinking of? Hint! It\'s \
between 1 and 10: ').strip())
i+=1
if pick_num<random_num:
print('\nThe number I\'m thinking of is too low!')
elif pick_num>random_num:
print('\nThe number I\'m thinking of is too high!')
elif pick_num==random_num:
print(f'\nCongratulations. You won! "{random_num}" was the number \
I was thinking of.')
break
except ValueError:
print('\nYou must type integers only please!')
else:
print('\nSorry. You lost!')
'''----------------------------------------------------------------'''
# Once again, this is the very same program example as above before.
# However, this time the loop iterates in reverse instead of forward and
# the user is shown how many guesses they have left before they win or
# lose.
# Note: the 'import random' module must be imported first.
import random
random_num=random.randint(1,10)
i=3
while i>0:
try:
pick_num=int(input(f'\nWhat number am I thinking of? Hint! It\'s \
between 1 and 10:\n\nYou have {i} gesses left. ').strip())
i-=1
if pick_num<random_num:
print('\nThe number I\'m thinking of is too low!')
elif pick_num>random_num:
print('\nThe number I\'m thinking of is too high!')
elif pick_num==random_num:
print(f'\nCongratulations. You won! "{random_num}" was the number \
I was thinking of.')
break
except ValueError:
print('\nYou must type integers only please!')
else:
print('\nSorry. You lost!')