Smallest Random Number Problem

My goal is to create a program that generates 10,000 random numbers between 0 and 25 and outputs the smallest number. I feel like I'm on the right track but need help. I believe the way I have it now might be giving me the first number generated, not necessarily smallest:

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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double random (unsigned int& seed);
double max_integer = 25;
double x, smallest;
int main()
{
    cout << fixed << setprecision(5) << endl;
    for (int i=0; i<=10000; ++i)
    {
        unsigned int seed;
        x = random(seed);
        if (x > 0)
            if (x < max_integer)
                smallest = x;
        
    }
    cout << "The smallest integer is: " << smallest << endl;
    return 0;
}

double random (unsigned int& seed)
{
    const int MODULUS = 15749;
    const int MULTIPLIER = 69069;
    const int INCREMENT = 1;
    seed = ((MULTIPLIER * seed) + INCREMENT)%MODULUS;
    return double(seed)/double(MODULUS);
}
First on line 7 you need to set smallest to max_integer.
Second line 16 should be: if (x < smallest)
Thanks for the help, coder777. Out of curiosity, how might I rearrange this program if I wanted the largest integer along with the smallest? Would it be something like:
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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double random (unsigned int& seed);

double x, y;
double max_integer = 25;
double min_integer = 0;
double smallest = max_integer;
double largest = min_integer;

int main()
{
    cout << fixed << setprecision(5) << endl;
    for (int i=0; i<=10000; ++i)
    {
        unsigned int seed;
        x = random(seed);
        y = random(seed);
        if (x > 0)
            if (x < smallest)
                smallest = x;
        if (y >= 0)
            if (y > largest)
                largest = y;
    }
    cout << "The smallest integer is: " << smallest << endl;
    cout << "The largest integer is: " << largest << endl;
    return 0;
}

double random (unsigned int& seed)
{
    const int MODULUS = 15749;
    const int MULTIPLIER = 69069;
    const int INCREMENT = 1;
    seed = ((MULTIPLIER * seed) + INCREMENT)%MODULUS;
    return double(seed)/double(MODULUS);
}


For some reason, it changes the value I get for x/smallest integer when I try to add y/largest.
Now you generate 20002 random numbers, not 10001 like in the original version.

Are the generated values "between 0 and 25"?
See: http://www.cplusplus.com/reference/cstdlib/rand/

(but preferably):
http://www.cplusplus.com/reference/random/uniform_int_distribution/
http://www.cplusplus.com/reference/random/uniform_real_distribution/
I'm trying to generate 10,000 random numbers between 0 and 25, and outputting the smallest and largest numbers generated. So no 20002
Last edited on
1. Loop from 0 to 10000 (inclusive) is 10001 steps.
2. Line 19 makes one number and line 20 does another. Two numbers per step.

10001 * 2 == 20002


However, the call of your random() does not produce values "between 0 and 25".
The possible max value is (MODULUS-1)/MODULUS. That is < 1.0.


Then the line 18. On every iteration you do create new, uninitialized variable "seed". Lets say that the platform has a custom to initialize it anyway, with "0". What does that do to the serie of "random" values?
Topic archived. No new replies allowed.