Random #

Pages: 12
i was wondering if there was a better way to generate a random number than
1
2
srand(time(0));
  rand() % 3 + 1;

because if you ask for two random numbers at the same second they will come up with the exact same number.
Only call srand once in your program, at the beginning of main is a good place, and you will not have this problem.
@brandonator

Try it this way..
1
2
3
#include <ctime>
time_t t;
srand((unsigned) time(&t));

I don't see how the two random numbers would be the same, though. Could you show the code that gives them? It would be helpful in determining the cause.
i know how to do random numbers but they will come up with the same number during the same second because its based on the second of time

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
#include <vector>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])technique.
{  
int x = 0;
int a;
do{
srand(time(0));
a=rand() % 3 + 1;
cout << a;
}while ( x != 1);
return 0;
}

note that something like this will come up with the same number each time, during the same second


would print something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
11111111111111111111111111111111111111111
11111111111111111111111111111111111111111
11111111111111111111111111111111111111111
11111111111111111111111111111111111111111
11111111111111111111111111111111112222222
22222222222222222222222222222222222222222
22222222222222222222222222222222222222222
22222222222222222222222222222222222222222
22222222222222222222222222223333333333333
33333333333333333333333333333333333333333
33333333333333333333333333333333333333333
33333333333333333333333333333333333333333
33333333333333333333333333333333333333333
33333333333311111111111111111111111111111
Last edited on
The random number generator rand isn't random. It just reads from a sequence. If the sequence starts with the same seed each time, it'll be the same sequence. You set the seed with srand.

If you set the seed the same way each time, with srand(time(0));, which is the same every time, you'll get the same sequence each time. Use srand ONCE.
Last edited on
@brandonator

That's because you're reseeding the random number generator by using the srand(time(0)) in the do loop. Move it outside the loop, then cout two or more random numbers. You should find they are different each time.
edited my post, please read, would your way fix this?
fixed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
#include <vector>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{  
int x = 0;
int a;
time_t t;
srand((unsigned) time(&t));
do{
a=rand() % 3 + 1;
cout << a;
x++;
}while ( x != 50);
cout << endl;
return 0;
}
Last edited on
@whitenight1 you did a slight error line 21 ( x != 50); should be ( x = 50); to keep the loop going, also can you explain what you did with the code, because now its a little better, still very predictable but better

123123123123123123123123123 ect.
Last edited on
you did a slight error line 21 ( x != 50); should be ( x = 50); to keep the loop going

Doing that would make the loop run forever. If that's what you wanted, you'd just have a while(1) at the top and be done with it.

If your implementation gives 123123123123123123123123123 then it's really, really bad.

The rand that typically comes with the compiler is bad. Really, really bad. If you want decent random numbers use the C++11 random number functions, or a decent random number generator from Boost or some other library.
Last edited on
yeah im wondering how to do that, btw i want the loop to continue going in the example to see the results

can you show me a better way to do random functions than these two examples

 
11111111111111111111122222222222222223333333333331111111111111

or
 
12312312312312312312312312312312312312312312312312312312312312


so it will actually come up with random (or more random results) such as
324596236423542934562356645235432542354325429423856243423

where theres not a predictable patturn but is chance for repitition

with the numbers 1-3
Last edited on
@brandonator

Please post the code you have now, that prints out the series 12312312312312312312312312312312312312312312312312312312312312
Also, what compiler are you using? I use MS Visual C++ 2010 Express
note the post
where theres not a predictable patturn but is chance for repitition


like in the middle it did 235664523 6 repeated

and324596236423542934562356645235432542354325429423856243423is not very predictable (typed random numbers)
and answer to question dev c++
Last edited on
@brandonator

I took your code, added a few small additions for readability, and compiled with Dev-C++. As you can see, it will print out 360 random numbers which should give you an idea of its randomness. Of course, there will always be the chance for repetition, especially with only having 3 random numbers to choose from.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
#include <vector>
#include <fstream>
using namespace std;
int main()
{  
int x = 0;
int a;
time_t t;
srand((unsigned) time(&t));
do{
a=rand() % 3 + 1;
cout << a << " "; // Put a space between each number, for better readability
x++;
if ( x%40 == 0)
cout << endl;
}while ( x != 360);
return 0;
}
sorry read your code and becausse of the speed i thought it was 123123123, when in reality it was more random (put a pause at the end), anyways, can you explain why your way comes up with better random numbers (line by line)
can you explain why your way comes up with better random numbers


Because he only calls srand once, at the start of his program. He does not call it for every random number like you were.



Only other thing I have to contribute is here:

1
2
3
4
5
6
7
// this is a little silly:
time_t t;
srand((unsigned) time(&t));

// there's no point is creating and filling 't' if you're not going to use it
//  just do this instead:
srand( (unsigned) time(0) );
yeah that was what was confusing me, i was trying to understand why

1
2
time_t t;
srand((unsigned) time(&t));


was working when

srand( (unsigned) time(0) );

was not, thats why the understandability of his program was confusing (when it was my mistake and those two made no difference)
Yes the only difference between those two pieces of code is that 't' holds the current time. Since you don't care about the time, you don't need 't'. There is zero difference as far as srand/rand goes.
@Disch

t holding the time makes ALL the difference, since it's the time that is seeding srand.
It makes no difference. The semantics of time() is
1
2
3
4
5
6
time_t time(time_t *t){
    time_t ct=/*current time*/;
    if (t)
        *t=ct;
    return ct;
}
Pages: 12