OSDev.org

The Place to Start for Operating System Developers
It is currently Sun May 05, 2024 6:57 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Psuedo Random Randomness...:lol:
PostPosted: Thu Apr 12, 2007 9:47 am 
Offline
Member
Member
User avatar

Joined: Sun Sep 18, 2005 11:00 pm
Posts: 106
Location: Williamstown, MA; Worcester, MA; Yokohama, Japan
On a mac there is a srandomdev(0) function that randomizes random(0) when I execute random again. (Does that make sense?) But on Linux I that function doesn't seem to exist, so before I go ahead and try to implement srandomdev(0) in my program I was wondering if there is a way to do this. Currently I just do srandom(random()) then random() but everytime I run the program I get the same results so it would be nice if I could somehow prevent that. My last solution would be to implement srandomdev() or somehow create my own random algorithm.

Thanks,
Kenneth


Top
 Profile  
 
 Post subject: Re: Psuedo Random Randomness...:lol:
PostPosted: Thu Apr 12, 2007 10:39 am 
Offline
Member
Member

Joined: Sun Jul 30, 2006 8:16 am
Posts: 149
Location: The Netherlands
Of course it's always the same, the randomizer you're using to seed your randomzier isn't properly seeded itself (since it's the one you're seeding), so it always produces the same "random" seed value.
Since you're on Linux, you could get your seed value by reading from /dev/urandom, or /dev/random if you're paranoid.
You could also use reading from one of those as your randomizer, but that could perform too much system calls if you need lots of numbers.
You should maybe be prepared for those files not to exist though, as IIRC there are some oddball distros with different locations for stuff. In that case you could use the old trick of using the current time as the seed, but since that's not really random it's probably best used as a fallback only.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 12, 2007 10:50 am 
Offline
Member
Member
User avatar

Joined: Tue Oct 17, 2006 11:33 pm
Posts: 3882
Location: Eindhoven
IIRC the function was called srand(), with rand() for random numbers. You can also make your own "random" number generator using for instance an LFSR.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 12, 2007 4:46 pm 
Offline
Member
Member
User avatar

Joined: Sun Sep 18, 2005 11:00 pm
Posts: 106
Location: Williamstown, MA; Worcester, MA; Yokohama, Japan
Gotcha! :D I might just implement my own random function and use that for both mac and linux and other OSs. The problem with date and time is that I am generating random numbers every millisecond (or maybe even every 10 or 100 nanoseconds - I'm not sure) so I might just do that :D Thanks!

Yours,
Kenneth


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 12, 2007 5:46 pm 
Offline
Member
Member
User avatar

Joined: Thu Mar 08, 2007 11:08 am
Posts: 670
You're supposed to seed your PRNG only once at the beginning of your program, so only if you actually start your program every 10 or 100 nanoseconds is the current time going to be insufficient, but if you do that, I think you've got worse problems to solve anyway. :)

edit: For encryption, you need to add more entropy to the system during the execution, but you can't use a seed that's easily guessed, (such as time), so the situation is much more complex anyway.

Also, with most PRNGs seeding the PRNG from it's own output will actually decrease entropy, so doing it is bad idea. The only common PRNG where this is not an issue is the classic linear congruent generator, when it's implement such that it returns the full state (that is, 32-bit state for a 32-bit return value), in which case seeding the last random number is basicly no-op (since the last value would have been used as a new seed anyway).

I'll leave it as an excersize to the reader to prove that it's impossible to design a PRNG for which srand(rand()) actually increases entropy.


Anyway, with random numbers it's important to ask yourself first: how random you need the values to be. For something like audio noise, the quality is more or less irrelevant (linear congruent is fine, speed is much more important). For stuff like simulation, you need high quality series on numbers, but it's actually a good thing if you can use the same seed several times (allowing you to repeat the same experiment).

For cryptography, you'd better forget about writing your own generator, as you not only need a high quality series, you also need to collect entropy from some unpredictable (external) source, and you must avoid leaking information about the generators internal state.

_________________
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: SemrushBot [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group