Computer Science Canada

random integers...help me please!

Author:  acidburn90 [ 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!!

Author:  Andy [ Thu May 05, 2005 7:50 am ]
Post 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

Author:  jamonathin [ Thu May 05, 2005 8:15 am ]
Post 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

Author:  md [ Thu May 05, 2005 10:50 am ]
Post 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.

Author:  wtd [ Thu May 05, 2005 11:42 am ]
Post 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());

Author:  jamonathin [ Thu May 05, 2005 1:24 pm ]
Post subject: 

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

Author:  wtd [ Thu May 05, 2005 1:41 pm ]
Post subject: 

Heh. Thanks. Smile

Author:  acidburn90 [ Thu May 05, 2005 4:36 pm ]
Post 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?

Author:  wtd [ Thu May 05, 2005 4:41 pm ]
Post 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) { ... }

Author:  acidburn90 [ Thu May 05, 2005 4:54 pm ]
Post subject: 

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

Author:  wtd [ Thu May 05, 2005 5:01 pm ]
Post 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.

Author:  acidburn90 [ Thu May 05, 2005 5:03 pm ]
Post 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??

Author:  wtd [ Thu May 05, 2005 5:05 pm ]
Post subject: 

Not really.

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

Author:  acidburn90 [ Thu May 05, 2005 5:45 pm ]
Post subject: 

oh.hmmmmm

Author:  jamonathin [ Thu May 05, 2005 7:31 pm ]
Post 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

Author:  rizzix [ Thu May 05, 2005 8:18 pm ]
Post subject: 

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

Author:  wtd [ Thu May 05, 2005 8:44 pm ]
Post subject: 

jamonathin wrote:
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


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:

code:
print_endline "hello world"


And compile it with:

code:
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.

Author:  jamonathin [ Fri May 06, 2005 1:23 pm ]
Post subject: 

Interesting . . .

wtd, what do you do for a living, because you seem know something about everything, you crazy |-|@X0|2

Author:  wtd [ Fri May 06, 2005 3:40 pm ]
Post subject: 

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

Author:  jamonathin [ Fri May 06, 2005 8:52 pm ]
Post subject: 

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

Author:  jamonathin [ Tue May 10, 2005 12:21 pm ]
Post subject: 

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
Turing:

randint (number, 1, 10)
%or
number := Rand.Int (1, 10)

<blonde moment>

Author:  Andy [ Tue May 10, 2005 6:35 pm ]
Post subject: 

why not actually read the help we've posted in the page before?

Author:  jamonathin [ Tue May 10, 2005 8:57 pm ]
Post subject: 

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.
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));
   int wait;
   cin >> wait;
   return 0;
}

But it still doesn't work.

Author:  wtd [ Tue May 10, 2005 9:02 pm ]
Post subject: 

What are you trying to do with:

code:
cout << (rand() % (m, n));


Oh, and please use space. It makes code more readable.

Author:  jamonathin [ Wed May 11, 2005 5:35 am ]
Post subject: 

Sorry, i juss copied and pasted. lol. All i wanted to do is find a random int between m and n, and i was trying to figure out how to do that using the program that finds a random int between a specific range. That program didn't work right and so thats why I was playing with it. I tried lookin it up on the net too, but im havin a brain fart from hell Blowing up

Author:  md [ Wed May 11, 2005 7:07 am ]
Post subject: 

Andy wrote:
...
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;
}


It looks like you copied this, but didn't quite get it right... I'm sure if you look closely you'll see the problem.

Author:  jamonathin [ Wed May 11, 2005 7:54 am ]
Post subject: 

Ah, i think I got it. drrrrr, lol. Yeah just that whole (m-n+1) thinger I didn't really understand, cuz I thought it ment more than it does, which is the max amount the rand can produce. Thanks for your help/patience Bday

c++:
//generate a random number between a specific range
#include <iostream>
using namespace std;
int main()
{
   int m,n;
   cin >> m >> n;
   cout << (rand() % (n))+ m;
   int wait;
   cin >> wait;
   return 0;
}


EDIT: nvm, it pulls up the same number every time, and if you restrict it to that number, it pulls up a different number every time.

Author:  wtd [ Wed May 11, 2005 10:13 am ]
Post subject: 

jamonathin wrote:
EDIT: nvm, it pulls up the same number every time, and if you restrict it to that number, it pulls up a different number every time.


Because you're no longer seeding it with the time.

Author:  jamonathin [ Thu May 12, 2005 8:36 am ]
Post subject: 

In conclusion I Hate Random Numbers Hit Wall . I got it to finally work (i had bran flakes this morning) so, sorry for the annoyance I caused you, but I thank you guys for puttin up with it Smile

Author:  betaflye [ Wed Jun 08, 2005 6:13 pm ]
Post subject: 

jamonathin wrote:

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


Lots of people learn machine code, machine code is worth learning, it should be meantioned it isn't portable among processors or even operating systems. Dev C++ is written in Delphi, afaik.

Author:  md [ Thu Jun 09, 2005 10:31 am ]
Post subject: 

betaflye wrote:

Lots of people learn machine code, machine code is worth learning, it should be meantioned it isn't portable among processors or even operating systems. Dev C++ is written in Delphi, afaik.

Machine code IS portable among operating systems on a specific processor if you don't call and OS specific functions (or if you only use posix calls, but that's gonna change with longhorn Sad)

As for learning machine language, no it is not worth learning unless you're writing an assembler. Assembly language on the other hand is worth learning, however you'd be hard pressed to actually write anything as complicated as Notepad in it. There is a reason that there are higher level languages, because without them things become extremely time consuming to write, and impossible to debug.

For example
c++:

    c = a + b;

vs.
2 instructions to load a and b into registers, 1 to add them, and one to copy the result back into memory in MIPS Assembler (sorry, don't remember the exact syntax... I'll try and look it up and post the code). Personally I much prefer the C++ to the Assembly, it's easy to understand and it's compact.

Author:  wtd [ Thu Jun 09, 2005 12:58 pm ]
Post subject: 

Cornflake wrote:
Personally I much prefer the C++ to the Assembly, it's easy to understand and it's compact.


Heh. C++ is higher-level than assembly is not exactly a ringing endorsement of C++. Wink

Author:  rizzix [ Thu Jun 09, 2005 2:19 pm ]
Post subject: 

betaflye wrote:
Dev C++ is written in Delphi, afaik.
Yes, and it is so damn buggy.

This is the same reason people hate BASIC, specially Visual Basic and its derivitives.


: