Created
January 5, 2015 12:32
-
-
Save rossng/52d370921036cbd3c027 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'ross' | |
import random | |
repeats = 10000 | |
intervals_per_repeat = 1000 | |
packet_types = [128,256] | |
input_links = ['a','b'] | |
def get_packet_size(input_link): # return a packet in the form ('a', 256) | |
if input_link == 'a': | |
prob_256 = 75 | |
elif input_link == 'b': | |
prob_256 = 64 | |
if random.randint(1,100) <= prob_256: | |
return 256 | |
else: | |
return 128 | |
def get_packet_stream(num_pairs): | |
packets = [] | |
for i in range(num_pairs): | |
first_link = random.choice(input_links) | |
if first_link == 'a': | |
second_link = 'b' | |
else: | |
second_link = 'a' | |
first_packet = (first_link, get_packet_size(first_link)) | |
second_packet = (second_link, get_packet_size(second_link)) | |
packets.append(first_packet) | |
packets.append(second_packet) | |
return packets | |
def get_adjacent_pair(packet_stream): # return an adjacent pair of packets from anywhere in the stream | |
packet_index = random.randint(0,len(packet_stream)-2) | |
packet_2_index = packet_index+1 | |
return [packet_stream[packet_index], packet_stream[packet_2_index]] | |
def run(num_repeats): | |
counter_ordered_packets = 0 # counter for the number of packet pairs which are in the correct order (256-byte then 128-byte) | |
counter_from_right_links = 0 # counter for the number of ordered packet pairs which also arrived on the correct links | |
for i in range(num_repeats): | |
stream = get_packet_stream(intervals_per_repeat) | |
packets = get_adjacent_pair(stream) | |
(link_1, size_1) = packets[0] | |
(link_2, size_2) = packets[1] | |
if size_1 == 256 and size_2 == 128: | |
counter_ordered_packets += 1 | |
if link_1 == 'a' and link_2 == 'b': | |
counter_from_right_links += 1 | |
print('prob from right links given in right order: ') | |
print(counter_from_right_links/counter_ordered_packets) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment