Advertisement
Guest User

PDP33_generator

a guest
Oct 28th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.73 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6. #include <errno.h>
  7.  
  8. #define LIMIT (999999999)
  9.  
  10. #ifndef N
  11.   #define N 50000L
  12. #endif
  13.  
  14. static uint16_t highest_bit(uint64_t v) {
  15.     uint16_t out = 0;
  16.     while (v > 0) {
  17.         v >>= 1;
  18.         ++out;
  19.     }
  20.     return out;
  21. }
  22.  
  23. uint32_t myrand() {
  24.     static bool init = 0;
  25.     static uint16_t n;
  26.     static uint16_t shift;
  27.     if (!init) {
  28.         uint16_t randbits = highest_bit(RAND_MAX + (uint64_t)1L);
  29.         uint16_t outbits = highest_bit(LIMIT);
  30.         n = (outbits + randbits - 1)/randbits;
  31.         shift = randbits;
  32.         init = 1;
  33.     }
  34.     uint32_t out = 0;
  35.     for (uint16_t i=0; i<n; ++i) {
  36.         out |= rand() << (i*shift);
  37.     }
  38.     return out % LIMIT;
  39. }
  40.  
  41. int main(int argc, char *argv[])
  42. {
  43. register long i, prev, code=1, cnt;
  44. int money, choose;
  45. char tr;
  46.  
  47. FILE *fp;
  48.  
  49.     if (argc < 2)
  50.     {
  51.         fp=fopen("testfile.txt","w+");
  52.         if (!fp)
  53.         {
  54.             perror("Opening output file...");
  55.             exit(1);
  56.         }
  57.     }
  58.     else
  59.     {
  60.         fp=fopen(argv[1],"w+");
  61.         if (!fp)
  62.         {
  63.             perror("Opening output file...");
  64.             exit(1);
  65.         }
  66.     }
  67.     srand(time(NULL));
  68.     fprintf(fp,"%ld\n", 2*N); /* Write something just to occupy first line */
  69.     for (i=0;i<N; i++)
  70.     {
  71.         choose = rand() % 2;
  72.         tr = (choose == 0 ? 'd' : 'w');
  73.         prev=code;
  74.         code=(long)myrand();
  75.         if (code==0) code=1;
  76.         money=rand();
  77.         if (tr=='w')
  78.         {
  79.             fprintf(fp,"%c %ld %d\n", tr, prev, money);
  80.             fprintf(fp,"d %ld %d\n", code, money);
  81.             cnt += 2;
  82.         }
  83.         else
  84.         {
  85.             fprintf(fp,"%c %ld %d\n", tr, code, money);
  86.             cnt++;
  87.         }
  88.         fprintf(fp,"q %ld\n", prev);
  89.         cnt++;
  90.     }
  91.     rewind(fp);
  92.     fprintf(fp, "%ld\n", cnt+1);   
  93.     fclose(fp);
  94.     return 0;
  95. }
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement