0

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?

5
Contributors
10
Replies
11
Views
6 Years
Discussion Span
Last Post by firstPerson
0

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;
 }
}
0

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

0

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

Edited by Jackk123: n/a

0

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;
}

Edited by astala27: n/a

0

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

0

>> 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 ).

0

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

0

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

0

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

0

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.

Edited by firstPerson: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.