-
Notifications
You must be signed in to change notification settings - Fork 0
/
myrandom.h
42 lines (38 loc) · 941 Bytes
/
myrandom.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// myrandom.h
// RandomWriter
//
// Created by Shanon Reckinger.
// Copyright © 2020 Shanon Reckinger. All rights reserved.
//
#pragma once
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
bool useAutograder = false;
int seed = 15;
/*
* Function: randomInteger
* Usage: int n = randomInteger(low, high);
* ----------------------------------------
* Returns a random integer in the range low to
* high, inclusive.
*/
int randomInteger(int low, int high) {
static bool initialized = false;
if (!initialized) {
if (useAutograder) {
srand(seed);
rand();
} else {
srand(int(time(NULL)));
rand(); // throwaway call to get randomness going
}
initialized = true;
}
double d = rand() / (double(RAND_MAX) + 1);
double s = d * (double(high) - low + 1);
return int(floor(low + s));
}