Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6605301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:16:04+00:00 2026-05-25T19:16:04+00:00

I need fast way to generate ip numbers that are valid (reserved ips are

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:16:04+00:00Added an answer on May 25, 2026 at 7:16 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with Android and I really need a fast way to get a
I need to check in some fast way if there is any text nodes
I need to generate a fast hash code in GetHashCode for a BitArray. I
I need a fast way in C# leanguage of converting/casting array of bytes encoding
I need to generate unique and consecutive numbers (for use on an invoice), in
Hi so I need some FAST way to search for a word in a
For my internship on Brain-Computer Interfacing I need to generate some very fast flickering
I need a simple and fast way to compare two images for similarity. I.e.
I need a very fast way to determine if an array consits only of
I need a fast way to create 24 bits bitmaps (and save to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.