
-----------------------------------
acidburn90
Wed May 04, 2005 6:51 pm

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!!

-----------------------------------
Andy
Thu May 05, 2005 7:50 am


-----------------------------------
in c++, the rand function calculates a number based on the internal clock of the system, so if we were to simply cout>m; cin>>n; 


And could you further explain why we need the

srand((unsigned)time(NULL)); 

Thanks  :)

-----------------------------------
md
Thu May 05, 2005 10:50 am


-----------------------------------
in c++, the rand function calculates a number based on the internal clock of the system, so if we were to simply cout>; and why are you trying to read a constant from input? >>endl is pointless, and will probably cause an error itself.

 And could you further explain why we need the
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
Thu May 05, 2005 11:42 am


-----------------------------------
std::cin >> m >> n;

Oh, and don't use C-style casts.  If you must use casts, instead use:

static_cast(foo());

-----------------------------------
jamonathin
Thu May 05, 2005 1:24 pm


-----------------------------------
Ok thanks guys.  P.S. I see you're finally at tony's clone wtd, congrats  :P

-----------------------------------
wtd
Thu May 05, 2005 1:41 pm


-----------------------------------
Heh.  Thanks.  :)

-----------------------------------
acidburn90
Thu May 05, 2005 4:36 pm


-----------------------------------
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?

-----------------------------------
wtd
Thu May 05, 2005 4:41 pm


-----------------------------------
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:

while (!false) { ... }

-----------------------------------
acidburn90
Thu May 05, 2005 4:54 pm


-----------------------------------
ahhhhhh.... so what about in dev c++....what do you put?

-----------------------------------
wtd
Thu May 05, 2005 5:01 pm


-----------------------------------
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
Thu May 05, 2005 5:03 pm


-----------------------------------
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
Thu May 05, 2005 5:05 pm


-----------------------------------
Not really.

C++ is a language.  Header files are, more or less just a way of defining words.

-----------------------------------
acidburn90
Thu May 05, 2005 5:45 pm


-----------------------------------
oh.hmmmmm

-----------------------------------
jamonathin
Thu May 05, 2005 7:31 pm


-----------------------------------
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  :?

-----------------------------------
rizzix
Thu May 05, 2005 8:18 pm


-----------------------------------
its not really all that confusing.. cuz the language is basically the abstraction of the internal on/off, true/false circuit system of the hardware. of course there are layers and layers of this abstraction for various reasons.. and the lowest lever a "software developer" can access is the assembly language.

But... its not necessary to know all the little details.. but a good broad view of the subject is sometimes helpful..

-----------------------------------
wtd
Thu May 05, 2005 8:44 pm


-----------------------------------
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  :?

Some people do know machine code.

A particular machine's internal "language" is devised by the people who create processors: IBM, Freescale, Intel, AMD, Via, etc.

But machine code is difficult to understand, so we have assembly language.  It's much easier to understand, write, and maintain.

It's not good enough.

That's why we have high-level languages.  

Also, if you write something in machine code directly, it'll run on exactly one kind of processor, and likely only one operating system.  Write it in a high-level language and you can compile it on any system that has a compiler for that language.

Consider that, for instance, I can write:

print_endline "hello world"

And compile it with:

ocamlopt hello_world.ml -o hw

On any of 9 platforms, and it'll go off without a hitch and print "hello world".  

This is called abstraction.  It's telling the computer what you want it to do, rather than how to do it.

-----------------------------------
jamonathin
Fri May 06, 2005 1:23 pm


-----------------------------------
Interesting . . .

wtd, what do you do for a living, because you seem know something about everything, you crazy |-|@X0|2

-----------------------------------
wtd
Fri May 06, 2005 3:40 pm


-----------------------------------
I don't.  I'm currently awaiting permission to work in Canada.

On a freelance basis I do web development from time to time.  My goal in life is to be a professional student.  :)

-----------------------------------
jamonathin
Fri May 06, 2005 8:52 pm


-----------------------------------
Are you an american, or some kinda terrorist?  If you're goal in life is to be a professional student, my goal is to be your professional teacher.  :P

-----------------------------------
jamonathin
Tue May 10, 2005 12:21 pm


-----------------------------------
Ok, I'm trying to do a program using random int's but I don't really understand the concept of it.  I thought i did earlier, but then I tried it and i was like, wtf mayte??  Is there an easy way of just doing

randint (number, 1, 10)
%or
number := Rand.Int (1, 10)



-----------------------------------
Andy
Tue May 10, 2005 6:35 pm


-----------------------------------
why not actually read the help we've posted in the page before?

-----------------------------------
jamonathin
Tue May 10, 2005 8:57 pm


-----------------------------------
I tried one of the programs you made, and I tried playing around with one of em, and I cant get anything out of it.  And what I got from the original code you made always gave me a number greater than the second number, never a number inbetween.  This is what I have so far.
//generate a random number between a specific range 
#include  
#include  
using namespace std; 
int main() 
{ 
   int m,n; 
   cin>>m>>n; 
  // srand((unsigned)time(NULL)); 
   cout wait;
   return 0; 
} 
But it still doesn't work.

-----------------------------------
wtd
Tue May 10, 2005 9:02 pm


-----------------------------------
What are you trying to do with:

cout 