Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-14-2003, 05:53 PM   #1
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++):
        ifstream randomStream;"/dev/random");
        char randomDataString [201];
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
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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 ...

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

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

        int randomInt;
        randomInt = int(randomChar);


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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:18 PM.

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