Question about Rand

Nov 17, 2012 at 4:40pm
What I'm trying to do is basically this, I want a certain rand function to be usable multiple times. For example:


Srand(time(0));
int x = rand()%100;
int z = 50;
while ( z == 50 )
cout<<" The Number is "<<x;
cout<<z-x;
break;
}
So here I want it to display the random number first, and then re-use it as z(50) - x(rand()%100). But what it does is make 2 completely different numbers.
How can I make a rand number and then use the same number again to subtract from a variable?
Last edited on Nov 17, 2012 at 4:41pm
Nov 17, 2012 at 5:08pm
Store the random number in a variable.
 
    int x = rand()%100;

The value of x won't change unless you do something which subsequently changes it.

I'm not sure what the actual question really is meant to be. The code fragment above doesn't compile, and the while statement doesn't make much sense.
Nov 17, 2012 at 5:16pm
Thanks for the reply, first of all.

I can post the the real code I'm having issues with if it helps. Basically x ( the rand function ) will only stay the same for one time. For example:

int x = rand()%100;
cout<<x;
cout<<x;

X will be a different number each time. I want some way to make X re-use the first rand number it generated. in a while loop.
Last edited on Nov 17, 2012 at 5:17pm
Nov 17, 2012 at 5:25pm
When I run
1
2
3
4
srand(time(NULL));
int x = rand()%100;
cout<<x << endl;
cout<<x;


The x is the same value for both cout's.
Nov 17, 2012 at 5:39pm
Xraze wrote:
Basically x ( the rand function ) will only stay the same for one time.

I'm not sure whether there's a meaning in there.
x is an integer. It isn't a function.

However, if you were to define a function, like this, then each call would return a different value.
1
2
3
4
5
6
7
8
9
10
int y()
{
    return rand()%100;
}
int main() 
{
    cout << y() << endl;
    cout << y() << endl;
    return 0;
}


But if you do this, the output will be the same each time:
1
2
3
    int a = y();
    cout << a << endl;
    cout << a << endl;

I do think you need to post all the actual code, as there's a lot of guessing and theorising here.
Last edited on Nov 17, 2012 at 5:40pm
Nov 17, 2012 at 5:42pm
Ok, now. If I have a class which has a .setValue() and .getValue().

How would I keep subtracting from the value in a loop using the rand function?

Example:

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
Class X {

private:

int z;

public: 

  z():

 z(100);
{
}

void setValue( int x ) {
     z = x ;
}
int getValue() {
  return x;
}

};


X char;

srand (time(NULL));

int e = rand()%100

while ( char.getValue() >1 ) {

cout<< " the random number is:  "<<e;

char.setValue(100-e);

}

Now, the char.setValue(100-e); will keep subtracting from a random number. but I want it to keep it's last subtraction. For example if the number is 50, the char.setValue should now hold only 50 instead of the 100 I declared at the start.

I know my question is difficult, but bear with me please.
Last edited on Nov 17, 2012 at 5:43pm
Nov 17, 2012 at 5:46pm
char.setValue(char.getValue()-e);

Although I will note that your getValue() won't compile. Please use actual code. Not junk you make up on the fly that doesn't compile.

[edit: And as noted previously, the value of e will not change from its original value.]
Last edited on Nov 17, 2012 at 5:50pm
Nov 17, 2012 at 5:47pm
Your question is not difficult.
But the code you posted above does not compile. How about just sharing the actual code which you are running and which gives you a problem.
Topic archived. No new replies allowed.