mac address generator help!

Hello,

I am trying to make an executable that will randomize MAC addresses. I have been stuck on making the correct format with the colons.
I want it to be like this XX:XX:XX but I can only get it with the first two pairs. Any feedback is appreciated. Thanks!
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
char letterAssign(int x)
{
        char l;
        if(x==10) l='A';
        if(x==11) l='B';
        if(x==12) l='C';
        if(x==13) l='D';
        if(x==14) l='E';
        if(x==15) l='F';

        return l;
}

string randomMac(string& mac)
{
        for(int i=0; i<12; i++)
        {
                int x=rand()%16;
                if(x>9)
                {
                        char letter=letterAssign(x);
                        mac+=letter;
                }
		else
		{
			mac+=std::to_string(x);
		}

		if(mac.length()==2) mac+=":";
		else if(mac.length()>2 && (mac.length()-1) %2==0) mac+=":";
		cout << mac.length() << endl;
        }
	return mac;
}
I found a solution if someone has a similar problem in the future.

I added this
1
2
                if(mac.length()<17 && mac.length()>2 && (i+1)%2==0)mac+=":";
                if(mac.length()==2) mac+=":";


To here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string randomMac(string& mac)
{
        for(int i=0; i<12; i++)
        {
                int x=rand()%16;
                if(x>9)
                {
                        char letter=letterAssign(x);
                        mac+=letter;
                }
                else
                {
                        mac+=std::to_string(x);
                }
                if(mac.length()<17 && mac.length()>2 && (i+1)%2==0)mac+=":";
                if(mac.length()==2) mac+=":";
        }
        return mac;
}
Here is another version...
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
#include <iostream>
#include <string>

std::string randomMac(void);
char letterAssign(int x);

int main()
{
std::cout << randomMac();
}

char letterAssign(int x)
{
        char l;
        if(x==10) l='A';
        if(x==11) l='B';
        if(x==12) l='C';
        if(x==13) l='D';
        if(x==14) l='E';
        if(x==15) l='F';

        return l;
}

std::string randomMac()
{
    std::string mac;
        for(int i=0; i<12; i++)
        {
            if(i>0?(i)%2==0:0) mac+=":";
            
            int x=rand()%16;
            if(x>9)
            {
                char letter=letterAssign(x);
                mac+=letter;
            }
	    	else
		    {
			    mac+=std::to_string(x);
		    }
        }
	return mac;
}

Ahh, fun!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>
#include <random>
#include <chrono>

int main()
{
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    std::default_random_engine generator(seed);
    std::uniform_int_distribution<int> distribution(0, 255);

    constexpr int N = 6;
    std::cout << std::setfill('0') << std::uppercase;
    for ( int i=0; i < N; ++i ) {
        if ( i ) std::cout << ':';
        std::cout << std::setw(2) << std::hex << distribution(generator);
    }
    std::cout << '\n';
    return 0;
}

However, real MAC's are not entirely random; some bits indicate whether the MAC is
unicast or multicast, local or globally unique, and first half usually reveals the vendor too.

There are both Python and Perl programs that take those details into account.
Topic archived. No new replies allowed.