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

  • Home
  • SEARCH
  • 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 702441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:43:17+00:00 2026-05-14T03:43:17+00:00

I want to use /dev/random or /dev/urandom in C. How can I do it?

  • 0

I want to use /dev/random or /dev/urandom in C. How can I do it? I don’t know how can I handle them in C, if someone knows please tell me how. 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-14T03:43:17+00:00Added an answer on May 14, 2026 at 3:43 am

    In general, it’s a better idea to avoid opening files to get random data, because of how many points of failure there are in the procedure.

    On recent Linux distributions, the getrandom system call can be used to get crypto-secure random numbers, and it cannot fail if GRND_RANDOM is not specified as a flag and the read amount is at most 256 bytes.

    As of October 2017, OpenBSD, Darwin and Linux (with -lbsd) now all have an implementation of arc4random that is crypto-secure and that cannot fail. That makes it a very attractive option:

    char myRandomData[50];
    arc4random_buf(myRandomData, sizeof myRandomData); // done!
    

    Otherwise, you can use the random devices as if they were files. You read from them and you get random data. I’m using open/read here, but fopen/fread would work just as well.

    int randomData = open("/dev/urandom", O_RDONLY);
    if (randomData < 0)
    {
        // something went wrong
    }
    else
    {
        char myRandomData[50];
        ssize_t result = read(randomData, myRandomData, sizeof myRandomData);
        if (result < 0)
        {
            // something went wrong
        }
    }
    

    You may read many more random bytes before closing the file descriptor. /dev/urandom never blocks and always fills in as many bytes as you’ve requested, unless the system call is interrupted by a signal. It is considered cryptographically secure and should be your go-to random device.

    /dev/random is more finicky. On most platforms, it can return fewer bytes than you’ve asked for and it can block if not enough bytes are available. This makes the error handling story more complex:

    int randomData = open("/dev/random", O_RDONLY);
    if (randomData < 0)
    {
        // something went wrong
    }
    else
    {
        char myRandomData[50];
        size_t randomDataLen = 0;
        while (randomDataLen < sizeof myRandomData)
        {
            ssize_t result = read(randomData, myRandomData + randomDataLen, (sizeof myRandomData) - randomDataLen);
            if (result < 0)
            {
                // something went wrong
            }
            randomDataLen += result;
        }
        close(randomData);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 375k
  • Answers 375k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The .lib file you linked is not what you think… May 14, 2026 at 8:07 pm
  • Editorial Team
    Editorial Team added an answer Here is a very simple description what you have to… May 14, 2026 at 8:07 pm
  • Editorial Team
    Editorial Team added an answer Try this: Dim date As Datetime = DateTime.ParseExact(_ yourString, "dd/MM/yyyy",… May 14, 2026 at 8:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.