0

This is my code:

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <crypt.h>
#include <string.h>
#include <stdlib.h>

int main(void){

int i;
unsigned long seed[2];

/* Generate a (not very) random seed */
seed[0] = time(NULL);
seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);

printf("Seed 0: %lu ; Seed 1: %lu", seed[0], seed[1]);
return 0;
}

I want to generate some very random seed that will be used into an hash function but i don't know how to do it!

polslinux
  • 1,739
  • 9
  • 34
  • 73

3 Answers3

3

Go for Mersenne Twister, it is a widely used pseudorandom number generator, since it is very fast, has a very long period and a very good distribution. Do not attempt to write your own implementation, use any of the available ones.

ClemKeirua
  • 539
  • 3
  • 9
3

You can read the random bits you need from /dev/random.

When read, the /dev/random device will only return random bytes within the estimated number of bits of noise in the entropy pool. /dev/random should be suitable for uses that need very high quality randomness such as one-time pad or key generation. When the entropy pool is empty, reads from /dev/random will block until additional environmental noise is gathered.(http://www.kernel.org/doc/man-pages/online/pages/man4/random.4.html)

int randomSrc = open("/dev/random", O_RDONLY);
unsigned long seed[2];
read(randomSrc , seed, 2 * sizeof(long) );
close(randomSrc);
tomato
  • 747
  • 5
  • 11
3

Because the algorithm is deterministic you can't get very random, only pseudo-random - for most cases what you have there is plenty, if you go overboard e.g.

Mac address + IP address + free space on HD + current free memory + epoch time in ms...

then you risk crippling the performance of your algorithm.

If your solution is interactive then you could set the user a short typing task and get them to generate the random data for you - measure the time between keystrokes and multiply that by the code of the key they pressed - even if they re-type the same string the timing will be off slightly - you could mix it up a bit, take mod 10 of the seconds when they start and only count those keystrokes.

But if you really really want 100% random numbers - then you could use the ANU Quantum Vacuum Random number generator - article

There is a project on GitHub it's pretty awesome way to beat the bad guys.

web_bod
  • 5,728
  • 1
  • 17
  • 25
  • This may seem random but could actually have a terrible distribution and not be as random as expected. – ClemKeirua May 17 '12 at 22:10
  • but the quantum random number generator is guaranteed - it takes Heisenberg's uncertainty principle - essentially that you can't know everything and asks "Well what happens if we have a perfect vacuum?" it turns out the Universe goes nuts spewing out virtual particles just to confound the scientists. – web_bod May 17 '12 at 22:14
  • 1
    Sorry, I was talking about your first solution (summing various elements). The quantuum number generator, like all the physically basec generators, might be a bit though to implement :) However it can indeed be a very good way to generate random numbers, since "real" efficient randomness (I mean not based on a deterministic algorithm) is based on principles of physics. http://www.random.org/ uses atmospheric noise for its number generation. – ClemKeirua May 17 '12 at 22:19
  • Thanks for the github's link ! I know they are only fetching the data on the servers using HTTP, but it might be of use. – ClemKeirua May 17 '12 at 22:23
  • I like random.org - great name – web_bod May 17 '12 at 22:26