Thread: using /dev/urandom multiple times

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    using /dev/urandom multiple times

    I want to get a value from /dev/urandom and use the value, then assign a new value from /dev/urandom to the same variable and use that new value. This will be done in an if inside a while-loop that will keep repeting

    I have tried using:
    Code:
    fp = fopen("/dev/random", "r");
    fclose(fp);
    and freopen() as well, but neither work

    I end up with something like this:
    Code:
    fp = fopen("/dev/random", "r");
    value = (int)fp;
    printf("%d", value); //if this prints 25
    fclose(fp);
    
    fp = fopen("/dev/random", "r");
    value = (int)fp;
    printf("%d", value); //then this prints 25
    fclose(fp);
    I want the second value to be completly different, and also random

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you doing this:
    Code:
    value = (int)fp;
    You're taking the value of the file pointer itself, not anything in the file. You're basically doing this:
    Code:
    int *foo;
    int bar = (int) foo;
    bar = (int) foo;
    bar = (int) foo;
    And wondering why they're all the same value.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    And since I am using a pointer do I use fscanf to get data instead?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Quote Originally Posted by miutsu View Post
    And since I am using a pointer do I use fscanf to get data instead?
    Actually I realy want a description of how to get these values since I plan on using them in a program to provide randomness

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Cprogramming.com FAQ > Work with files (C)

    Read the FAQ on working with files. Or the section of your book that covers it.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I think you need to use fread(). man page fread section 3

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Quote Originally Posted by Bayint Naung View Post
    I think you need to use fread(). man page fread section 3
    Thank you, that worked
    I used:
    Code:
    fp = fopen("/dev/urandom", "r");
    fread(&random, sizeof(int), 1, fp);
    random = abs(random);
    and it worked perfectly, but I dont have any man pages on fread or any other C functions, do you know where I could download those, I have Ubuntu.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Splitting source into multiple files(Linux & make)
    By IceDane in forum C Programming
    Replies: 6
    Last Post: 05-18-2009, 07:31 AM
  2. Multiple Definition Error
    By timmeh in forum C++ Programming
    Replies: 9
    Last Post: 02-15-2009, 12:25 PM
  3. using malloc multiple times on one variable
    By Bladactania in forum C Programming
    Replies: 8
    Last Post: 02-13-2009, 11:53 AM
  4. Avoiding multiple includes
    By plan7 in forum C Programming
    Replies: 5
    Last Post: 11-25-2007, 06:13 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM