Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 random integers...help me please!
Index -> Programming, C++ -> C++ Help
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
acidburn90




PostPosted: Wed May 04, 2005 6:51 pm   Post subject: random integers...help me please!

hey... im kinda new to this... but i was wondering if anyone could tell me how to write random integer commands... and give me a breif tutorial on them... i have dev cpp... i tried looking for them on the help... but i got totally lost.. and it didnt help!!
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Thu May 05, 2005 7:50 am   Post subject: (No subject)

in c++, the rand function calculates a number based on the internal clock of the system, so if we were to simply cout<< rand(); we'd get the same numbers over and over again, thus, we'd have to set a random starting point first before we generate the numbers.

c++:

#include <iostream>
#include <ctime>
using namespace std;
int main()
{
   srand((unsigned)time(NULL));
   cout<< rand();
   return 0;
}


this will generate a random number from 0 to RAND_MAX. to get it between a range, all you need to do is mod the number for example, if we want a random number from 0 to 4, we'd have this

c++:

#include <iostream>
#include <ctime>
using namespace std;
int main()
{
   srand((unsigned)time(NULL));
   cout<< rand() % 5;
   return 0;
}

since by modular math, any number mod 5 will be congruent to 0 1 2 3 or 4, so for a range from 0 to 10. simply mod the rand by 11

if u want a sepcific range from n to m to then find out how many numbers there are in the range by taking (rand() % (m-n+1))+n

c++:

//generate a random number between a specific range
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
   int m,n;
   cin>>m>>n;
   srand((unsigned)time(NULL));
   cout<<(rand() % (m-n+1))+n;
   return 0;
}

hope this helps Smile
jamonathin




PostPosted: Thu May 05, 2005 8:15 am   Post subject: (No subject)

c++:

   cin<<m,n<<endl;


There are some errors on this line, even if you cahnge the << to >>, it still says its wrong. Is there a way to "cin" multiple int's on the same line, or is it just like this?
c++:

    cin>>m; cin>>n;


And could you further explain why we need the
c++:

srand((unsigned)time(NULL));

Thanks Smile
md




PostPosted: Thu May 05, 2005 10:50 am   Post subject: (No subject)

Andy wrote:
in c++, the rand function calculates a number based on the internal clock of the system, so if we were to simply cout<< rand(); we'd get the same numbers over and over again, thus, we'd have to set a random starting point first before we generate the numbers.


Well... rand() actually only produces the same numbers if you use the same seed, and the default seed is just some number. By calling srand() you can set the seed, and in most cases you set it to the time.

jamonathin wrote:
c++:


   cin<<m,n<<endl;

There are some errors on this line, even if you cahnge the << to >>, it still says its wrong. Is there a way to "cin" multiple int's on the same line, or is it just like this?
c++:

    cin>>m; cin>>n;



You can read many things at once from cin. Problem is that << is generally considered to be output (pointing to the stream), and >> is input (getting from the stream), also you don't separate different variables by commas, but by >>; and why are you trying to read a constant from input? >>endl is pointless, and will probably cause an error itself.

jamonathin wrote:
And could you further explain why we need the
c++:

srand((unsigned)time(NULL));


Thats to set the seed for hte random number generator, by default the random number uses the last number returned (or somepart therein) to generate the next number, or a set number if it is the first call. This number is called the seed, and if you know the seed for the first number in a given string of random numbers you can reproduce it (bad). The solution is to set the seed to be a fairly random number (in this case the time that the call is executed). To do this we call srand(), passing it a new seed value.

If you want to generate the same sequence of numbers over and over again you can set the seed to a constant.

**note: you only need to set the seed once, setting it before every call will cause flaky stuff to happen.
wtd




PostPosted: Thu May 05, 2005 11:42 am   Post subject: (No subject)

c++:
std::cin >> m >> n;


Oh, and don't use C-style casts. If you must use casts, instead use:

c++:
static_cast<unsigned long>(foo());
jamonathin




PostPosted: Thu May 05, 2005 1:24 pm   Post subject: (No subject)

Ok thanks guys. P.S. I see you're finally at tony's clone wtd, congrats Razz
wtd




PostPosted: Thu May 05, 2005 1:41 pm   Post subject: (No subject)

Heh. Thanks. Smile
acidburn90




PostPosted: Thu May 05, 2005 4:36 pm   Post subject: (No subject)

yeh thnx guys!!! t helped a lot... just one more question though... in dev c++ 5 ... if i write '!=' that means 'not equal to' right?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu May 05, 2005 4:41 pm   Post subject: (No subject)

acidburn90 wrote:
yeh thnx guys!!! t helped a lot... just one more question though... in dev c++ 5 ... if i write '!=' that means 'not equal to' right?


Yes. But that's no a feature of Dev-C++. Rather it's simply part of the C++ language. ! means "not". So we could have something like:

c++:
while (!false) { ... }
acidburn90




PostPosted: Thu May 05, 2005 4:54 pm   Post subject: (No subject)

ahhhhhh.... so what about in dev c++....what do you put?
wtd




PostPosted: Thu May 05, 2005 5:01 pm   Post subject: (No subject)

acidburn90 wrote:
ahhhhhh.... so what about in dev c++....what do you put?


It hels to understand what C++ is.

C++ is a language that has been standardized. It exists independent of any particular piece of software. The capabilities of Dev-C++, Microsoft Visual C++, GCC, etc. don't define C++.

They simply provide a way of turning C++ source code (the stuff humans can read) into machine code. Dev-C++ isn't even a compiler. Rather it's a program that simply uses the GCC compiler and advanced ways to edit and organize C++ source code.
acidburn90




PostPosted: Thu May 05, 2005 5:03 pm   Post subject: (No subject)

isee ... so with different header files you have different languages... is that what you mean... and c++ is just a program that is able to read that language... that kinda thing??
wtd




PostPosted: Thu May 05, 2005 5:05 pm   Post subject: (No subject)

Not really.

C++ is a language. Header files are, more or less just a way of defining words.
acidburn90




PostPosted: Thu May 05, 2005 5:45 pm   Post subject: (No subject)

oh.hmmmmm
jamonathin




PostPosted: Thu May 05, 2005 7:31 pm   Post subject: (No subject)

wtd wrote:
They simply provide a way of turning C++ source code (the stuff humans can read) into machine code. Dev-C++ isn't even a compiler. Rather it's a program that simply uses the GCC compiler and advanced ways to edit and organize C++ source code.

Thats messed up. So why dont people ever learn the machine code, isn't it kinda like cheating then to use other programs? Because how do people write the programs such as Dev C++, by machine code?..... then who invented the machine code, and how did they write that?? it's all messed up to me Confused
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 33 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: