Q. Suppose that after we import the random module, we define the following function called diff in a Python session:
import random
def diff():
x = random.random() - random.random()
return(x)
What would be the result you now evaluate?
y = diff ()
print(y)
At the Python prompt? Give reasons for your answer.
Answer =
It will give the output subtracted number which is generated by random() function. Because random() function generate number between 0.0 <= N < 1.0
Some Output for better understanding :-
1 :- -0.08899059831340761
2 :- -0.2641307615748565
3 :- -0.15186117416700495
4 :- 0.5870958926221614
5 :- -0.2991020123554199
6 :- -0.45002944998189875
7 :- 0.4451795771419502
8 :- -0.33269355525380184
9 :- 0.5233832432345172
10 :- 0.7344604783929822
11 :- -0.028943564053555915
12 :- 0.16439666433585232
13 :- 0.317636935547817
14 :- 0.44553269551591845
15 :- 0.015251629144331424
16 :- 0.7722477175533216
17 :- 0.0807607571525909
18 :- -0.19780025106287358
19 :- -0.6512724035723381
20 :- -0.19621143322562284
21 :- -0.8873166055441549
22 :- -0.37079230501758675
23 :- -0.012913835806743101
24 :- 0.3038462912028028
25 :- -0.0569014338024979
26 :- -0.15045140374071397
27 :- -0.20514096935129555
28 :- 0.2749349342382562
29 :- 0.2195598123152971
30 :- 0.2864659293539621
31 :- 0.0020003659796330453
32 :- -0.26333435183003484
33 :- 0.2603235786925564
34 :- 0.5864791308718348
35 :- -0.34370551272037597
36 :- -0.6989819667988909
37 :- 0.13587891222331894
38 :- 0.12798341040417194
39 :- -0.06006837593122005
40 :- -0.020941316180658087
41 :- 0.3685826496714467
42 :- -0.5481646226089292
43 :- -0.32101939740686736
44 :- 0.7690879070269044
45 :- 0.4617059793960374
46 :- -0.01567810888965271
47 :- 0.2938084457377379
48 :- 0.7963510213756534
49 :- 0.3776569511633776
50 :- 0.6026907983808092
51 :- -0.4645343436359939
52 :- 0.09246271684865981
53 :- 0.33929236137662844
54 :- -0.45436488460802993
55 :- -0.3162836377604257
56 :- 0.2371042977119595
57 :- -0.23864315352676613
58 :- 0.08549600373547384
59 :- 0.4268612648745115
60 :- 0.010642423736502704
61 :- -0.8680676998728768
62 :- -0.6497320135997704
63 :- -0.25611959875915524
64 :- 0.04661442907891489
65 :- -0.33677947975960654
66 :- 0.5782747346604402
67 :- 0.642824969324424
68 :- 0.019571296358947277
69 :- 0.292719040415521
70 :- -0.49574999785534646
71 :- -0.0052403617274083825
72 :- -0.3514797919574346
73 :- 0.24318644994122385
74 :- -0.4689885086460496
75 :- 0.0030274740898672547
76 :- -0.9114980104720262
77 :- -0.5021968162269186
78 :- 0.3473953756783549
79 :- 0.4276490952976083
80 :- -0.15490754885590852
81 :- 0.9354293681962358
82 :- 0.1368607966390598
83 :- 0.2289369195518698
84 :- 0.17930748854294487
85 :- 0.31328075095305497
86 :- -0.25973246463624033
87 :- 0.04436015061018217
88 :- -0.42274351990269443
89 :- 0.1965569155379837
90 :- -0.3695184856852263
91 :- -0.0590596959908023
92 :- -0.2998284321184145
93 :- 0.7095715984932236
94 :- 0.7719400187155329
95 :- -0.21533912303460734
96 :- 0.6902199668118129
97 :- -0.37372076373709495
98 :- 0.6033248052944022
99 :- -0.3641489369194252
100 :- 0.32866375110293833
>>>
Alternate answer : The output will be between (-2,2) as random.random() gives value between 0 and 1.
ReplyDelete*between (0,2)
DeleteYou are wrong, Please read examples.
DeleteCan zero be the output of above questions
ReplyDeleteYes, but it will very rar.
DeletePls reduce the examples for 100 to 3
ReplyDeleteI have uploaded for better understanding.
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )