+ 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?

7th Mar 2020, 2:18 PM
Armina
Armina - avatar
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.
7th Mar 2020, 2:27 PM
Shadow
Shadow - avatar
+ 5
Armina thanks
9th Mar 2020, 12:26 PM
Bruno Félix
Bruno Félix - avatar
+ 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.
7th Mar 2020, 2:58 PM
Shadow
Shadow - avatar
+ 4
random values ​​range from 0 to what value?
9th Mar 2020, 11:33 AM
Bruno Félix
Bruno Félix - avatar
+ 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/
7th Mar 2020, 2:36 PM
Armina
Armina - avatar
+ 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; } }
8th Mar 2020, 9:35 PM
Josip Susnjara
Josip Susnjara - avatar
+ 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)?
7th Mar 2020, 2:45 PM
Armina
Armina - avatar
+ 2
Pradeep Kumar Prajapati Josip Susnjara Thanks for your explanation 🙏🙏
9th Mar 2020, 4:39 AM
Armina
Armina - avatar
+ 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.
9th Mar 2020, 11:41 AM
Armina
Armina - avatar
+ 1
~ swim ~ Thanks for a million. You helped me alot.
7th Mar 2020, 3:56 PM
Armina
Armina - avatar
+ 1
Shadow Thanks alot for your explanation
7th Mar 2020, 3:57 PM
Armina
Armina - avatar
+ 1
put srand(time(NULL)); in your main function
8th Mar 2020, 4:27 AM
Erick Thomas
Erick Thomas - avatar
+ 1
and make sure to also use #include <ctime>
8th Mar 2020, 5:44 AM
Erick Thomas
Erick Thomas - avatar
+ 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?
8th Mar 2020, 5:51 AM
Armina
Armina - avatar
+ 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.
8th Mar 2020, 6:28 AM
Armina
Armina - avatar
+ 1
~ swim ~ Thanks alot.
8th Mar 2020, 6:36 AM
Armina
Armina - avatar
8th Mar 2020, 4:34 PM
Pradeep Kumar Prajapati
Pradeep Kumar Prajapati - avatar
+ 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.
9th Mar 2020, 2:48 AM
Pradeep Kumar Prajapati
Pradeep Kumar Prajapati - avatar
+ 1
BRUNO FELIX DE SOUSA You're welcome
9th Mar 2020, 12:40 PM
Armina
Armina - avatar
+ 1
https://www.sololearn.com/compiler-playground/c1L05XgGpj1i generate random number from 1 to 10 and you have to guess it
27th Sep 2022, 12:25 AM
Oliver Hsiao