Reputation: 685
I need fast way to generate ip numbers that are valid (reserved ips are valid too). For now i am using this:
unsigned char *p_ip;
unsigned long ul_dst;
p_ip = (unsigned char*) &ul_dst;
for(int i=0;i<sizeof(unsigned long);i++)
*p_ip++ = rand()%255;
ip.sin_addr.s_addr = ul_dst;
But sometimes it generate non-valid numbers, but this code can generate about 10k of valid ips in a second. Can anyone contribute? Thank you
Upvotes: 1
Views: 5930
Reputation: 637
Building over @Serdalis idea of using init_rand(), maybe you can try something I had come across called Knuth Shuffle which will help you generate Random Numbers with a uniform Distribution.
The code will look like this I guess now but the below example uses rand():
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define TOTAL_VAL_COUNT 254
int byteval_array[TOTAL_VAL_COUNT] = {
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, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
131, 132, 133, 134, 135, 136, 137, 138, 139, 140,
141, 142, 143, 144, 145, 146, 147, 148, 149, 150,
151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
161, 162, 163, 164, 165, 166, 167, 168, 169, 170,
171, 172, 173, 174, 175, 176, 177, 178, 179, 180,
181, 182, 183, 184, 185, 186, 187, 188, 189, 190,
191, 192, 193, 194, 195, 196, 197, 198, 199, 200,
201, 202, 203, 204, 205, 206, 207, 208, 209, 210,
211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
221, 222, 223, 224, 225, 226, 227, 228, 229, 230,
231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
241, 242, 243, 244, 245, 246, 247, 248, 249, 250,
251, 252, 253, 254
};
unsigned char denominator = TOTAL_VAL_COUNT+1;
unsigned char generate_byte_val();
unsigned char generate_byte_val() {
unsigned char inx, random_val;
if (denominator == 1)
denominator = TOTAL_VAL_COUNT+1;
inx = rand() % denominator;
random_val = byteval_array[inx];
byteval_array[inx] = byteval_array[--denominator];
byteval_array[denominator] = random_val;
return random_val;
}
int main(int argc, char **argv) {
int i;
struct in_addr ip;
for (i = 1; i < 255; ++i) {
ip.s_addr = (generate_byte_val() |
(generate_byte_val() << 8) |
(generate_byte_val() << 16) |
(generate_byte_val() << 24));
printf ("IP = %s\n", inet_ntoa(ip));
}
}
HTH
Upvotes: 0
Reputation: 10489
calling rand() is probably the slowest part of your code, if you use the implementation of a random function found at http://en.wikipedia.org/wiki/Multiply-with-carry
This is an ultra fast C function for generating random numbers.
storing sizeof(unsigned long)
in a registered variable i.e.:
register int size = sizeof(unsigned long)
should also help slightly.
Since you are using 4 chars = 4 x 8 byte memory, you can instead use a 32bit integer which will only require one memory address. combining the bitshifting, new random method, registered variables, should reduce running times by quite a bit.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
uint32_t ul_dst;
init_rand(time(NULL));
uint32_t random_num = rand_cmwc();
ul_dst = (random_num >> 24 & 0xFF) << 24 |
(random_num >> 16 & 0xFF) << 16 |
(random_num >> 8 & 0xFF) << 8 |
(random_num & 0xFF);
printf("%u\n",ul_dst);
return 0;
Above this code I have the exact copy of the random function from wikipedia. Hopefully this will run much faster. We know the size of a 32bit int is 4*8 so no need for the sizeof anymore, and instead of %255 I replaced it with a 255 bit mask
Upvotes: 6
Reputation: 44250
Roll your own random generator. For this purpose anything with a period of (1<<32) is valid, so you could construct a lineair congruential thing. (you would not need to construct from 4 separate characters, too)
Also, your *p_ip is uninitialised. you probably want a
p_ip = (unsigned char *) &ul_dst;
somewhere.
Upvotes: 1
Reputation: 75609
Currently, your code writes four chars to memory. You can optimize this by writing one int32 to memory.
Upvotes: 1