can any one please tell me am i done my question right?
my question is to store store 1035 to 1265 random in a character array and display the array.
and im not sure whats this srand is doing?

#include<iostream>
#include<stdlib>
#include<conio.h>
int main(){
srand(1035);
char b[30];
for(int i=0;i<30;i++)   {
b[i]=(1+rand()%(1265)); 
}
for(int i=0;i<10;i++)   {
cout<<b[i];
}
getch(); 
return 0;}

Edited 5 Years Ago by AmerJamil: n/a

srand() is ued to seed the random number generator. If you call srand() with the same number every time your program is run rand() will generate the same sequence of random numbers, which is the default if the program never calls srand(). To get rand() to generate a different set of random numbers each time the program is run you need to call srand() with a different seed number. The easiest way to do that is to call time() function from time.h, like this: srand( time(0) ); you might have to typecast the return value of time() to unsigned int if your compiler complains about it.

What srand() does not do: that function does NOT set the lower value of the numbers returned by rand(), as you are implying in your program. See the examples here to find out the correct way to do that.

Edited 5 Years Ago by Ancient Dragon: n/a

my question is to store 1035 to 1265 random in a character array and display the array.

Yes, I know what you question was -- but you asked if it was done. How am I supposed to know if you are finished with it or not?

I already told you your code is wrong and how to fix it. You have not posted your corrected code. And you can easily check it yourself by compiling and running it. When you run that program it will tell you whether it is right or wrong.

This article has been dead for over six months. Start a new discussion instead.