Привет
я опаздал?!
у меня есть примерчик из книги на всякий случай#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
int random_number(int min_number, int max_number);
int main(int argc, char* argv[])
{
int min_number;
int max_number;
int r_number;
if(argc<2){
cout << "Usage: random_number min_number max_number\n";
exit(1);
}
min_number = atoi(argv[1]);
max_number = atoi(argv[2]);
r_number = random_number(min_number, max_number);
cout << "random number is " << r_number << "\n";
return 0;
}
int random_number(int min, int max)
{
static int dev_random_fd = -1;
char* next_random_byte;
int bytes_to_read;
unsigned random_value;
assert(max > min);
if(dev_random_fd == -1){
dev_random_fd = open("/dev/random", O_RDONLY);
assert(dev_random_fd != -1);
}
next_random_byte = (char*) &random_value;
bytes_to_read = sizeof (random_value);
do{
int bytes_read;
bytes_read = read(dev_random_fd, next_random_byte, bytes_to_read);
bytes_to_read -= bytes_read;
next_random_byte += bytes_read;
}while(bytes_to_read > 0);
return min + (random_value % (max - min +1));
}