tuxmartin

C generovani nahodnych cisel 2

Dec 6th, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. FILE *fp;
  7. FILE *csv;
  8.    
  9. int generujNahodneCislo() {
  10.     int byte_count = sizeof(int);
  11.     int random[1];
  12.     fread(&random, 1, byte_count, fp);
  13.     //printf("%d\n", random[0]);
  14.     //printf("%.10f\n", (double) random[0] / 10000000000);
  15.     return random[0];
  16. }
  17.  
  18. int main(int argc, char *argv[]) {
  19.     fp = fopen("/dev/urandom", "r");
  20.     csv = fopen("dev_urandom.csv", "w+");
  21.     time_t start, stop;
  22.  
  23.     int kolik = 1;
  24.    
  25.     if (argc > 1) {
  26.         kolik = atol(argv[1]);
  27.     }
  28.  
  29.     printf("Kolik: %d\n", kolik);
  30.  
  31.     int cisla[kolik];
  32.  
  33.     time(&start);
  34.     for (int i=0; i< kolik; i++) {
  35.         cisla[i] = generujNahodneCislo();
  36.     }
  37.     time(&stop);
  38.  
  39.     for (int i=0; i< kolik; i++) { 
  40.         //fprintf(csv, "%d,%d\n", i, cisla[i]);
  41.         double abc = (double) cisla[i] / 10000000000;
  42.         fprintf(csv, "%.10f\n", abc);
  43.         printf("Nahodne cislo c. %d = %.10f\n", i, abc);
  44.     }
  45.  
  46.     printf("Vygenerovano: %d cisel\n", kolik);
  47.     printf("Generovani trvalo %.000fs\n", difftime(stop, start));
  48.  
  49.     fclose(fp);
  50.     fclose(csv);
  51.  
  52.     return 0;
  53. }
  54.  
  55. // gcc -o dev_urandom dev_urandom.c -std=c99
  56.  
  57. /* VYSTUP:
  58.  
  59. Kolik: 10
  60. Nahodne cislo c. 0 = 0.1952607124
  61. Nahodne cislo c. 1 = 0.1485520581
  62. Nahodne cislo c. 2 = 0.1176588805
  63. Nahodne cislo c. 3 = 0.0430723194
  64. Nahodne cislo c. 4 = 0.1302624476
  65. Nahodne cislo c. 5 = -0.1477905392
  66. Nahodne cislo c. 6 = -0.2100886278
  67. Nahodne cislo c. 7 = -0.1149736942
  68. Nahodne cislo c. 8 = -0.0067722964
  69. Nahodne cislo c. 9 = 0.1352800048
  70. Vygenerovano: 10 cisel
  71. Generovani trvalo 0s
  72.  
  73. */
Advertisement
Add Comment
Please, Sign In to add comment