/** Seed using a single value. */ void seed( int32_t iseed ) { #if 0 THIS IS COMMENTED OUT BECAUSE THE OLD Crappy VERSION DID NOT DO THIS AND WE NEED TO COMPARE THE UPDATE. AFTER THIS COMPARISON, WE CAN REENABLE THIS LINE. /* We need to make sure that iseed is positive, because this generator * works on ints and not uints. */ iseed = std::abs(iseed); #endif /* First time, fill in the state */ // Burn the first 100 lines for ( unsigned int ii = 0; ii < 100; ++ii ) iseed = simple_rand(iseed); state[0] = simple_rand(iseed); for ( unsigned int i = 1; i < (state_length-1); ++i ) state[i] = simple_rand(state[i-1]); state[state_length-1] = 0; /* FOLLOWING THE OLD Crappy VERSION, WE BURN THROUGH THE FIRST NUMBER * HERE: */ static_cast<void>( randInt() ); }
/** integer in [1,2^31 - 2]. * TODO: fix documentation; is the range correct? */ uint32_t randInt() { int32_t & st_last = state[state_length-1]; uint32_t chosen = simple_rand(st_last); st_last = ((state_length-1)*chosen)/mod; chosen = state[st_last]; state[st_last] = simple_rand(chosen); return chosen; }
static int rand_offs(void) { int offs; if (bufsize < 32768) offs = simple_rand(); else offs = (simple_rand() << 15) | simple_rand(); offs %= bufsize; return offs; }
static int rand_len(int offs) { int len; if (bufsize < 32768) len = simple_rand(); else len = (simple_rand() << 15) | simple_rand(); len %= (bufsize - offs); return len; }
void mix_index(double randomness) { double r; long i; for(i = 0 ; i < N_ACCESSES; i++) { if (simple_rand() >= randomness) j[i]=i; else { r = simple_rand() * 2.0 * (double)N_ACCESSES; j[i] = (long) r; } } }
static int rand_eb(void) { int eb; again: if (ebcnt < 32768) eb = simple_rand(); else eb = (simple_rand() << 15) | simple_rand(); /* Read or write up 2 eraseblocks at a time - hence 'ebcnt - 1' */ eb %= (ebcnt - 1); if (bbt[eb]) goto again; return eb; }
static int rand_eb(void) { int eb; again: if (ebcnt < 32768) eb = simple_rand(); else eb = (simple_rand() << 15) | simple_rand(); eb %= (ebcnt - 1); if (bbt[eb]) goto again; return eb; }
static int do_operation(void) { if (simple_rand() & 1) return do_read(); else return do_write(); }
static void set_random_data(unsigned char *buf, size_t len) { size_t i; for (i = 0; i < len; ++i) buf[i] = simple_rand(); }
int main(int argc, const char *argv[]) { long int x, y, g; printf("Shamir\n"); for (int i = 0; i < 5; i++) { x = simple_rand(); y = shamir(x); printf("sent:%ld, received:%ld %s\n", x, y, x == y ? "PASSED" : "FAILED"); } printf("\nVernam\n"); char f_name[] = "files/test_file"; if (vernam_e(f_name)) { printf("Vernam encode ERROR\n"); } f_name[15] = '\0'; if (vernam_d(f_name)) { printf("Vernam decode ERROR\n"); } if (!system("diff files/test_file files/test_file.vrm.key.s > /dev/null")) { printf("PASSED\n"); } else { printf("FAILED\n"); } printf("\nEl Gamal\n"); char f_name_elg[] = "files/test_file"; if (elgamal_e(f_name_elg)) { printf("RSA encode ERROR\n"); } f_name_elg[15] = '\0'; if (elgamal_d(f_name_elg)) { printf("RSA decode ERROR\n"); } if (!system("diff files/test_file files/test_file.elg.s > /dev/null")) { printf("PASSED\n"); } else { printf("FAILED\n"); } printf("\nRSA\n"); char f_name_rsa[] = "files/test_file"; if (RSA_e(f_name_rsa)) { printf("RSA encode ERROR\n"); } f_name_rsa[15] = '\0'; if (RSA_d(f_name_rsa)) { printf("RSA decode ERROR\n"); } if (!system("diff files/test_file files/test_file.rsa.s > /dev/null")) { printf("PASSED\n"); } else { printf("FAILED\n"); } return 0; }
/* return a random uint64_t */ uint64_t simple_rand_i ( ) { uint64_t i = 0 ; for (int k = 0 ; k < 5 ; k++) { i = SIMPLE_RAND_MAX * i + simple_rand ( ) ; } return (i) ; }
int rand_255 (void) { /* return a random number between 0 and 255 */ long int temp = 256; while (temp > 255) { temp = simple_rand (); temp %= 257; } return temp; }
int main() { srand(0); for(int i = 0;i < max_depth; ++i) { test_rand_data[i] = simple_rand(); } for(int i = 0;i < 1000;i ++) { if (fabs(rt(i * 0.01f,0) - rt_non_recursive(i * 0.01f)) < 0.00001f) { } else std::cout<<"not equal "<<i<<": "<<rt(i * 0.01f,0)<<" "<< rt_non_recursive(i * 0.01f)<<std::endl; //std::cout<<<<std::endl; } }
void Deck::shuffle() { std::minstd_rand simple_rand; auto seconds = std::time(nullptr); simple_rand.seed(seconds); // credit to: http://codereview.stackexchange.com/questions/39001/fisher-yates-modern-shuffle-algorithm auto currentIndexCounter = mDeck.size(); for (auto iter = mDeck.rbegin(); iter != mDeck.rend(); iter++, --currentIndexCounter) { int randomIndex = simple_rand() % currentIndexCounter; if (*iter != mDeck.at(randomIndex)) { std::swap(mDeck.at(randomIndex), *iter); } } }
/* return a random double between 0 and 1, inclusive */ double simple_rand_x ( ) { return (((double) simple_rand ( )) / ((double) SIMPLE_RAND_MAX)) ; }
/* * Use simple random number generator. */ int game_rand(game *g) { /* Call simple random number generator */ return simple_rand(&g->random_seed); }
static int __init mtd_stresstest_init(void) { int err; int i, op; uint64_t tmp; printk(KERN_INFO "\n"); printk(KERN_INFO "=================================================\n"); printk(PRINT_PREF "MTD device: %d\n", dev); mtd = get_mtd_device(NULL, dev); if (IS_ERR(mtd)) { err = PTR_ERR(mtd); printk(PRINT_PREF "error: cannot get MTD device\n"); return err; } if (mtd->writesize == 1) { printk(PRINT_PREF "not NAND flash, assume page size is 512 " "bytes.\n"); pgsize = 512; } else pgsize = mtd->writesize; tmp = mtd->size; do_div(tmp, mtd->erasesize); ebcnt = tmp; pgcnt = mtd->erasesize / mtd->writesize; printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, " "page size %u, count of eraseblocks %u, pages per " "eraseblock %u, OOB size %u\n", (unsigned long long)mtd->size, mtd->erasesize, pgsize, ebcnt, pgcnt, mtd->oobsize); /* Read or write up 2 eraseblocks at a time */ bufsize = mtd->erasesize * 2; err = -ENOMEM; readbuf = vmalloc(bufsize); writebuf = vmalloc(bufsize); offsets = kmalloc(ebcnt * sizeof(int), GFP_KERNEL); if (!readbuf || !writebuf || !offsets) { printk(PRINT_PREF "error: cannot allocate memory\n"); goto out; } for (i = 0; i < ebcnt; i++) offsets[i] = mtd->erasesize; simple_srand(current->pid); for (i = 0; i < bufsize; i++) writebuf[i] = simple_rand(); err = scan_for_bad_eraseblocks(); if (err) goto out; /* Do operations */ printk(PRINT_PREF "doing operations\n"); for (op = 0; op < count; op++) { if ((op & 1023) == 0) printk(PRINT_PREF "%d operations done\n", op); err = do_operation(); if (err) goto out; cond_resched(); } printk(PRINT_PREF "finished, %d operations done\n", op); out: kfree(offsets); kfree(bbt); vfree(writebuf); vfree(readbuf); put_mtd_device(mtd); if (err) printk(PRINT_PREF "error %d occurred\n", err); printk(KERN_INFO "=================================================\n"); return err; }