Member Avatar

I know there are a lot of topcs.
I was searching for what i needed.But as i sow there wasn't anything that i can specifically use.
I need a short code that generates 10 random numbers.
Anyone help?

That's nothing a quick google can't help you with. But since you asked here it is :

#include <iostream>
#include <ctime>
using namespace std;

int main(){
 srand(time(0));
 for(int i = 0; i != 10; ++i){ 
   cout << rand() << endl;
 }
}

do you need just the function to generate the random numbers or would you like to
know how a rand() like function is working?

Member Avatar

The random numbers
rand() is quite easy if u ask me
but the code that first person gave me doesnt work for me
it crashes

You could use something like this;

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{        //As I remember srand() gave us a begining value to count.
          srand(time(0));
          //expression after rand() funct. represents numbers are between 20 and 10. 
          int i = rand() % 20 - 10; 

          cout << "Random number between 10 and 20 is " << i << "." << endl;

          return 0;
}
Member Avatar

No astala27
i dont need a random number between 10 and 20
READ my post!!!
i need a function to generate 10 RANDOM numbers

>> i need a function to generate 10 RANDOM numbers

so initialize a integer array of size 10 and fill each of them one by one using
the rand.

>>but the code that first person gave me doesnt work for me
it crashes

it works fine for me, if it crashed give me the stack trace,then I'll see what
I can do next.(Dev C++/mingw ).

WOW! You guys are pretty patient and generous with this poster who shouts at someone who offers him help rather than thanks that person.

Member Avatar

Ok Nathaniel0 wtf
does !!! mean shouting -.-
learn grammar dude

Member Avatar

It worked.
I dont know why it crashed the first time.
Thanx people.

Hey Jackk, I don't know if you been drinking Jack or something, but you need to watch
your manners. This isn't your house, so you don't get to shout or demand.