From a625cf7110bea62767733f80189b317a195b4cd0 Mon Sep 17 00:00:00 2001 From: Shrey03 Date: Sat, 15 Jan 2022 23:25:52 -0500 Subject: [PATCH] commit 12 --- Random.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Random.py diff --git a/Random.py b/Random.py new file mode 100644 index 0000000..166c010 --- /dev/null +++ b/Random.py @@ -0,0 +1,49 @@ +# An efficient python3 program +# to randomly select a number +# from stream of numbers. +import random + +# A function to randomly select a item +# from stream[0], stream[1], .. stream[i-1] +def selectRandom(x): + + # The resultant random number + res = 0; + + # Count of numbers visited + # so far in stream + count = 0; + + # increment count of numbers + # seen so far + count += 1; + + # If this is the first element + # from stream, return it + if (count == 1): + res = x; + else: + + # Generate a random number + # from 0 to count - 1 + i = random.randrange(count); + + # Replace the prev random number + # with new number with 1/count + # probability + if (i == count - 1): + res = x; + return res; + +# Driver Code +stream = [1, 2, 3, 4]; +n = len(stream); + +# Use a different seed value +# for every run. +for i in range (n): + print("Random number from first", + (i + 1), "numbers is", + selectRandom(stream[i])); + +# This code is contributed by mits