LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-14-2003, 05:53 PM   #1
PTBmilo
Member
 
Registered: Jan 2003
Posts: 167

Rep: Reputation: 30
using /dev/(u)random for seed ?


What is a good way to do this? this is what I'm trying (c++):
Code:
        ifstream randomStream;
        randomStream.open("/dev/random");
        char randomDataString [201];
        randomStream.getline(randomDataString,200);
And then I try to conver the string to a number with some gmp functions. I can print out the datastring, but I can only get '0' when I convert it to an integer.

I can make integers with regular character strings, but this one won't work. Are there any other ways of doing this?
 
Old 04-14-2003, 08:29 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Not quite sure why you'd want to read a 200
characters from the device ... what are you
trying to achieve?

One "normal approach" in using it would be
to read ONE byte from it, and use that as an
integer as a seed for srand()...
Or, if you want repeated random numbers from
0 to 254, repeatedly read ONE byte ...

Cheers,
Tink
 
Old 04-15-2003, 01:19 AM   #3
PTBmilo
Member
 
Registered: Jan 2003
Posts: 167

Original Poster
Rep: Reputation: 30
Thanks this is how I ended up doing it:
Code:
int getRandomInt()
{
        ifstream randomStream;
        randomStream.open("/dev/urandom");
        if(randomStream.fail())
        {
                cout << "Can't open /dev/urandom\n";
                exit (1);
        }
        char randomChar;
        randomStream.get(randomChar);

        int randomInt;
        randomInt = int(randomChar);

        randomStream.close();

return randomInt;
}
Then use that for a seed. It's not for anything important, I'm just playing around w/ the gmp library. I wanted to use a big ass binary string for the initial seed, but something wasn't flying.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
system hanging on 'Saving random seed' PennyroyalFrog Linux - Newbie 9 10-01-2005 02:45 PM
Kernel panic while saving random seed SAnton Linux - Software 1 09-27-2005 03:01 AM
/dev/(u)random not returning anything... Elric of Grans Linux - General 1 09-02-2005 01:12 AM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 12:12 PM
doubt in dev/random varala_kanth Linux - Software 0 07-06-2004 07:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration