+ 2
rand() function in c++
#include <iostream> #include <cstdlib> using namespace std; int main() { for (int x = 1; x <= 10; x++) { cout << rand() << endl; } } Why the above code gives random numbers? Doesn't it use the same seed? If we don't use a loop it gives the same number , so what's the difference with loop ? Can anyone tell me the reason please?
28 ответов
+ 5
The code does not produce truly random numbers. Try running the program twice, you will see the exact same sequence of numbers, since the same seed it used, as you said. The sequence itself might appear random, that is because when a number is generated, it is then internally used as the new seed for the random function, thus generating ten different numbers, however, with the same starting seed the sequence as a whole will not be random and always produce the same numbers.
+ 5
Armina thanks
+ 4
The lession mentions that rand() is a pseudo-random number generator, i.e. it as an deterministic algorithm that attempts to generate a sequence of numbers whose properties approximate those of a random number sequence. It achieves this by doing complex calculations based off a seed, and then using the generated number as the next seed. Thus, the same seed will result in the exact same sequence. To generate more truly random numbers, you often use a seed as unique as possible, such as the system time, since it is fairy unlikely a program is started at the exact same timestamp very often. The rand() function uses srand() for this purpose. The argument given to srand() is used as the very first seed for the sequence. The default seed without a call to srand() is one, I believe. Seeding a generator more than once with the same value will result in the state of the generator returning to that of the given value, meaning it will produce the same numbers again.
+ 4
random values range from 0 to what value?
+ 3
Shadow
This code is actually from one of the sololearn leassen learning c++.
In the lessen it is said that this code will generate random numbers.
This is the lesson link:
https://www.sololearn.com/learn/CPlusPlus/1638/
+ 3
It gives random numbers, but with each program run it will give the same sequence of numbers.
To prevent this, you need to give it a seed which depends on the current time, and thus ensuring the sequence will be different at every program execution.
The following changes are required:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
rand();
for (int x = 1; x <= 10; x++) {
cout << rand() << endl;
}
}
+ 2
~ swim ~
When we just cout<<rand(); it generates the same number evey time we run it.
I used the <cstdlib> header but i don't understand why a loop generate random numbers (doesn't use the same seed)?
+ 2
Pradeep Kumar Prajapati
Josip Susnjara
Thanks for your explanation 🙏🙏
+ 2
BRUNO FELIX DE SOUSA
They range from 0 to RAND_MAX.
RAND_MAX is a constant defined in <cstdlib> header file.
RAND_MAX is actually INT_MAX.
This value s a library-department, but is guaranteed to be at least 32767 at any standard library implementation.
+ 1
~ swim ~
Thanks for a million. You helped me alot.
+ 1
Shadow
Thanks alot for your explanation
+ 1
put srand(time(NULL)); in your main function
+ 1
and make sure to also use
#include <ctime>
+ 1
~ swim ~
I thought NULL means it has nothing in it
while 0 is an integer. Is it wrong?
also what does #define NULL 0 mean?
+ 1
~ swim ~
So it both case : Using nullptr for pointers and for time(null ptr) , nullptr will bereplaced by value 0 in the preprocessing stage??
Sorry for the number of my questions, I'm new to progmaming.
+ 1
~ swim ~
Thanks alot.
+ 1
https://code.sololearn.com/cweQ4I2GR43N/?ref=app
works good here
+ 1
well if you are using turbo compiler then the rand() will always give same results.
if you put srand() before rand() and call the rand() multiple times it will give different results but if you recompile the program and call rand() multiple times again, the results are same as the first compilation.
if you use stand(time(0)) then the rand() will produce truly random number on each call and each compilation.
based on my experience on turbo 4.0 compiler. the sololearn code playground may have different behavior.
+ 1
BRUNO FELIX DE SOUSA
You're welcome
+ 1
https://www.sololearn.com/compiler-playground/c1L05XgGpj1i
generate random number from 1 to 10 and you have to guess it