random number always the same???

Hello!
When I made program with:
int x=rand();
it is always generating the same nnumber for the same variable.
It is devcpp compiler.

What am I supposed to do?
Want just with each RUN to get other numbers...
Many thanks!

you need srand(time(0));
Thanks!
For this code:

1
2
3
4
5
6
7
8
9
#include<iostream>
#include<stdlib.h>
using namespace std;
int main(){
int x=rand();
int y=srand(time(0));
cout<<x<<endl<<y;
return0;
}


I got next error:



"In function 'int main()':
Line 6: error: void value not ignored as it ought to be
compilation terminated due to -Wfatal-errors."

How should I write it???
Many thanks!

Hi! I would try using this code. Hope this helped.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstdlib>
using namespace std;


int main()
{
    srand (time(NULL));
    
    int x = rand();

    cout << x << endl;
    
    return 0;
}
Alarm!!!
Please, why are generated numbers pretty same?
Many thanks!!!
ps. where I put x=rand()%10; I sure KNOW why, but I did nto put it for all variables!

Why are those OTHER variables too similar???
Many thanks!!!
Last edited on
#include <ctime> <-- Don't forget to include the ctime library!
It does not respect my %10 in

x=rand()%10

any more!
Is generating bigger and also negative numbers!!!

Post your complete source code so that we see where the mistake may be.
i dont know how to fix any of this because im still new myself but here is the num gen i use
1
2
3
4
#include <cstdlib>
#include <ctime>
srand(time(0));
int r = rand() % (20 - 10) + 10;


that's just from 10-19 u can change the code for what you want
Topic archived. No new replies allowed.