Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdint.h>
- #include <time.h>
- #include <stdlib.h>
- #include <errno.h>
- #define LIMIT (999999999)
- #ifndef N
- #define N 50000L
- #endif
- static uint16_t highest_bit(uint64_t v) {
- uint16_t out = 0;
- while (v > 0) {
- v >>= 1;
- ++out;
- }
- return out;
- }
- uint32_t myrand() {
- static bool init = 0;
- static uint16_t n;
- static uint16_t shift;
- if (!init) {
- uint16_t randbits = highest_bit(RAND_MAX + (uint64_t)1L);
- uint16_t outbits = highest_bit(LIMIT);
- n = (outbits + randbits - 1)/randbits;
- shift = randbits;
- init = 1;
- }
- uint32_t out = 0;
- for (uint16_t i=0; i<n; ++i) {
- out |= rand() << (i*shift);
- }
- return out % LIMIT;
- }
- int main(int argc, char *argv[])
- {
- register long i, prev, code=1, cnt;
- int money, choose;
- char tr;
- FILE *fp;
- if (argc < 2)
- {
- fp=fopen("testfile.txt","w+");
- if (!fp)
- {
- perror("Opening output file...");
- exit(1);
- }
- }
- else
- {
- fp=fopen(argv[1],"w+");
- if (!fp)
- {
- perror("Opening output file...");
- exit(1);
- }
- }
- srand(time(NULL));
- fprintf(fp,"%ld\n", 2*N); /* Write something just to occupy first line */
- for (i=0;i<N; i++)
- {
- choose = rand() % 2;
- tr = (choose == 0 ? 'd' : 'w');
- prev=code;
- code=(long)myrand();
- if (code==0) code=1;
- money=rand();
- if (tr=='w')
- {
- fprintf(fp,"%c %ld %d\n", tr, prev, money);
- fprintf(fp,"d %ld %d\n", code, money);
- cnt += 2;
- }
- else
- {
- fprintf(fp,"%c %ld %d\n", tr, code, money);
- cnt++;
- }
- fprintf(fp,"q %ld\n", prev);
- cnt++;
- }
- rewind(fp);
- fprintf(fp, "%ld\n", cnt+1);
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement