Getting truley random in C |
Getting truley random in C |
Mar 31 2006, 04:35 PM
Post
#1
|
|
|
Staff ![]() Group: V.I.P. Posts: 295 Joined: 20-November 04 From: /usr/bin Member No.: 3672 |
Pseudo random, I am not
CODE int ssrand(int min, int max) { //This creates a function called ssrand(min, max) which generates a random int between min and max FILE* f; unsigned q; float randmax=65536; float x; f=fopen("/dev/urandom", "r"); fread(&q, 1, 4, f); randmax *= randmax; x = q / randmax; x = x * (max-min); x= x + min; fclose(f); return x; } I needed to write this for a phone line tester, i figured someone will eventualy need it. This function will read for the urandom device and return a truly random int between MIN and MAX from your computers "entropy pool". The data from urandom is truly random because it is calculated using the disorderin mouse movements and keystrokes. To see this open a terminal and type CODE cat /dev/random This will dump the current entropy pool to the screen. Now type CODE cat /dev/urandom That will dump the current entropy pool and automaticly [and mst importantly, randomly] create more. -------------------- Win·dows Noun. A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.
|
|
|
|
|
|
|
Valirian_Oak Getting truley random in C Mar 31 2006, 04:35 PM
ChefNinja But you see, calculating random numbers from mouse... Mar 31 2006, 05:46 PM
Valirian_Oak This is very close to being truly random, for even... Apr 1 2006, 02:48 AM
psyba What about if you automated this? Would it work i... Apr 1 2006, 09:00 AM
Siph0n QUOTE(psyba @ Apr 1 2006, 02:00 PM) What ... Apr 1 2006, 09:36 AM
Subterraneus well, if it only gets it's entropy from user i... Apr 1 2006, 09:31 AM
Subterraneus O.o
interesting...I think Apr 1 2006, 09:40 AM
madruse You really shouldn't say truly random. It is r... Apr 1 2006, 09:42 AM
Siph0n QUOTE(madruse @ Apr 1 2006, 02:42 PM) You... Apr 1 2006, 09:46 AM
madruse QUOTE(Siph0n @ Apr 1 2006, 08:46 AM) But ... Apr 1 2006, 09:50 AM
Siph0n QUOTE(madruse @ Apr 1 2006, 02:50 PM) Ent... Apr 1 2006, 11:02 AM
madruse QUOTE(Siph0n @ Apr 1 2006, 10:02 AM) Sure... Apr 1 2006, 11:22 AM
Siph0n QUOTE(madruse @ Apr 1 2006, 04:22 PM) Act... Apr 1 2006, 12:00 PM
Valirian_Oak QUOTEActually, I was asking how you know entropy e... Apr 2 2006, 01:07 AM
madruse QUOTE(Valirian_Oak @ Apr 2 2006, 01:07 AM... Apr 3 2006, 12:45 AM
Peradox Ah, guys. urandom is from more than just keyboard ... Apr 28 2006, 07:26 PM
Valirian_Oak From Man Pages
QUOTEThe character special files /... Apr 28 2006, 09:28 PM
Poiuyt I quickly scanned over this post and didn't re... May 3 2006, 03:45 PM
Siph0n here an implementation..i think...
CODE#!/usr... May 3 2006, 10:00 PM![]() ![]() |
| Lo-Fi Version | Time is now: 7th September 2010 - 10:09 PM |