random function problem

Why isn't my program giving me random numbers each time I print

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
43
44
45
46
47
48
49
50
51
52
53
54
55
  double random(unsigned long int & seed)
{
    const int MODULUS = 15749;
    const int MULTIPLIER = 69069;
    const int INCREMENT = 1;
    seed = ((MULTIPLIER*seed)+INCREMENT)%MODULUS;
    return double (seed)/MODULUS;
    
}
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
    const int SIZE = 10000;
    unsigned long int seed;
    double x,y,z,f1,f2,f3,new_t;
    double old_t=100;
  
    f1= random(seed);
    f2= random(seed);
    f3= random(seed);
    
    x= 15*f1/(f1+f2+f3);
    y= 15*f2/(f1+f2+f3);
    z= 15*f3/(f1+f2+f3);
    old_t= sqrt(8*8 + x*x)/3 + y/5 + sqrt(6*6 + z*z)/4;
    
    for (int i=0; i<SIZE; i++)
{
    f1= random(seed);
    f2= random(seed);
    f3= random(seed);
    
    x= 15*f1/(f1+f2+f3);
    y= 15*f2/(f1+f2+f3);
    z= 15*f3/(f1+f2+f3);
    new_t= sqrt(8*8 + x*x)/3 + y/5 + sqrt(6*6 + z*z)/4;
    
    if(new_t < old_t)
{
    cout << setprecision(2);
    cout << "x = " <<  setw(5) << x << endl;
    cout << "y = " <<  setw(5) << y  << endl;
    cout << "z = " <<  setw(5) << z  << endl;
    cout << "t = " <<  setw(5) << old_t << endl;
    cout << endl;
    old_t=new_t;
}
}
    return 0;
}
Every time you print? What does that mean? If you mean every time you run the program then you need to initialize the seed value with the current time (or something). As it is, you aren't initializing the seed value at all, which is an error.
I randomized the seed but when I run the program my values aren't randomized when I run it.
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
double random(unsigned long int & seed)
{
    const int MODULUS = 15749;
    const int MULTIPLIER = 69069;
    const int INCREMENT = 1;
    seed = ((MULTIPLIER*seed)+INCREMENT)%MODULUS;
    return double (seed)/MODULUS;
    
}
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    const int SIZE = 10000;
    unsigned long int seed;
    srand(time(0));
    double x,y,z,f1,f2,f3,new_t;
    double old_t=100;
  
    f1= random(seed);
    f2= random(seed);
    f3= random(seed);
    
    x= 15*f1/(f1+f2+f3);
    y= 15*f2/(f1+f2+f3);
    z= 15*f3/(f1+f2+f3);
    old_t= sqrt(8*8 + x*x)/3 + y/5 + sqrt(6*6 + z*z)/4;
    
    for (int i=0; i<SIZE; i++)
{
    f1= random(seed);
    f2= random(seed);
    f3= random(seed);
    
    x= 15*f1/(f1+f2+f3);
    y= 15*f2/(f1+f2+f3);
    z= 15*f3/(f1+f2+f3);
    new_t= sqrt(8*8 + x*x)/3 + y/5 + sqrt(6*6 + z*z)/4;
    
    if(new_t < old_t)
{
    cout << setprecision(2);
    cout << "x = " <<  setw(5) << x << endl;
    cout << "y = " <<  setw(5) << y  << endl;
    cout << "z = " <<  setw(5) << z  << endl;
    cout << "t = " <<  setw(5) << old_t << endl;
    cout << endl;
    old_t=new_t;
}
}
    return 0;
}
closed account (E0p9LyTq)
I randomized the seed

You time seeded the C library's rand() function, not your randomize() function.
Last edited on
how would I do this
put seed = rand() after srand call.
seed is technically randomish because its not initialized, but in truth, it probably is zero a lot -- uninitialized values are usually just the value in ram that was there before, from whatever was in that space before, and its often just zeros or FFs or 0C0C (microsoftism) or some other predictable value. Don't rely on it to be terribly random.
Topic archived. No new replies allowed.