User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » random generation question (python code) Page [1]  
raiden
All American
10505 Posts
user info
edit post

So I have this python script that I've written to randomly generate the host portion of mac address. The range of addresses is 00:00:00 - 7F:FF:FF, which is about 8,388,608 possibilities.

So what this is supposed to do is write each mac to a file, and then check said file for duplicates. However this isn't happening, even on one iteration, and still not happening when I run 565 iterations.

Here's the code:

#!/usr/bin/python

import random
import os

def randomMAC():
mac = [ 0x54, 0x52, 0x00,
random.randint(0x00, 0x7f),
random.randint(0x00, 0xff),
random.randint(0x00, 0xff) ]
return ':' .join(map(lambda x: "%02x" % x, mac))

def writeMAC(randomMAC):
f = open('macaddresses.txt','w')
for k in range(1,566):
#s = str(randomMAC())
#f.write(s)
print >>f, randomMAC()
f.close()

def Iteration(writeMAC):
for i in range(1,8388608):
writeMAC(randomMAC)
os.system('echo "Iteration "' + str(i) + '>> duplicatereport.txt && cat macaddresses.txt | uniq -cd >> duplicatereport.txt 2>&1')
os.remove('macaddresses.txt')

#for k in range(565, 0, -1):
# for i in range(8388607, 0, -1):
# print randomMAC()

#f = open('holler.txt','w')
#for i in range(8388607, 0, -1):
# print >>f, randomMAC()
#f.close()
#os.system('echo "ITERATION 1" && cat holler.txt | uniq -cd > duplicatereport.txt 2>&1')
#os.remove('holler.txt')

#for i in range(1,6):
# os.system('echo "Iteration "' + str(i) + '>> duplicatereport.txt && cat macaddresses.txt | uniq -cd >> duplicatereport.txt 2>&1')

Iteration(writeMAC)


I know I'm missing something, b/c statistically speaking, I should be getting some duplicates, due to the birthday paradox.

Any thoughts/suggestions?

1/11/2011 1:44:10 PM

darkone
(\/) (;,,,;) (\/)
11610 Posts
user info
edit post

By my math, you need a set of approximately 3600 before you can expect a collision.

1/11/2011 2:10:01 PM

raiden
All American
10505 Posts
user info
edit post

Yeah I'm thinking there should be duplicates occurring at a higher frequency than what my script is producing.

1/11/2011 2:18:22 PM

moron
All American
34142 Posts
user info
edit post

It looks like a limitation in the "uniq" command.

I generated a file with 4 million MAC addresses, changed the last 2 to be the same, and it found just them.

I then changed maybe the 20th MAC address down in the list, and it did not pick it up as a duplicate.

Looks like you're writing your own duplicate-finding function...

[Edited on January 11, 2011 at 2:43 PM. Reason : ]

1/11/2011 2:43:46 PM

raiden
All American
10505 Posts
user info
edit post

shit, I was hoping that wouldn't be the case. Well, time to pound out some code.

1/11/2011 5:17:34 PM

darkone
(\/) (;,,,;) (\/)
11610 Posts
user info
edit post

uniq requires the input to be sorted

1/11/2011 6:29:26 PM

llama
All American
841 Posts
user info
edit post

just pipe it though sort before uniq

from the manpage:
Quote :
"Discard all but one of successive identical lines from INPUT"

1/11/2011 8:39:11 PM

raiden
All American
10505 Posts
user info
edit post

yeah, I'm piping it through sort at the moment, and its finding lots of collisions/duplicates.

thanks for the heads up tdub!

1/13/2011 10:31:44 AM

 Message Boards » Tech Talk » random generation question (python code) Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.39 - our disclaimer.