C++ help.

If it doesn't fit elsewhere, it should go here
Post Reply
User avatar
Hen Barrison
Posts: 157
Joined: 02 Feb 2012, 23:45

Post » 20 Feb 2012, 04:23

BeIng the aspIrIng programmer I am, I was fIddleIng wIth the numbers and operatIons of thIs random number generator I found onlIne. I tInkered wIth It long enough to get the output to be truly random, but Its output Is InfInite. I want It to generate one number at a tIme, or at least somethIng more manageable.

Here's what I've got so far.

Code: Select all

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    srand((unsigned)time(0));
    int random_integer;
    int lowest=1, highest=20;
    int range=(highest-lowest)+1;
    for(int index=0; index<20; index--){
        random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
        cout << random_integer << endl;

    }
}
P.S,

I really have an absoulty novIce understandIng of what I'm workIng wIth here, so If the answer Is burnIngly obvIous, I apologIze.

D.P.S, also, If It matters, I'm usIng code blocks as my complIer.

User avatar
HeroKing
Posts: 104
Joined: 10 Feb 2012, 23:57

Post » 20 Feb 2012, 05:43

code blocks, nice choice 8) same one i use. if you're trying just a random number generator, that is way too complicated. i tried it, and all i got was the number 1 in an infinite loop. for a short and simple random number generator, here's what you want



i highly recommend this video tutorial series, since it's the one i follow, and easiest for me to understand. here's a playlist, so you can add it to your account, if you wish to do so.

http://www.youtube.com/playlist?list=PL96ED5F4FC5EA8F7D

User avatar
Hen Barrison
Posts: 157
Joined: 02 Feb 2012, 23:45

Post » 20 Feb 2012, 06:14

HeroKing wrote:code blocks, nice choice 8) same one i use. if you're trying just a random number generator, that is way too complicated. i tried it, and all i got was the number 1 in an infinite loop. for a short and simple random number generator, here's what you want

[youtube][youtube]

i highly recommend this video tutorial series, since it's the one i follow, and easiest for me to understand. here's a playlist, so you can add it to your account, if you wish to do so.
Thanks, that works perfectly.

User avatar
Hen Barrison
Posts: 157
Joined: 02 Feb 2012, 23:45

Post » 20 Feb 2012, 07:42

Well, Now that I have that, I want to prInt text, based on what number the generator gives out.

How would I do that?

Here's what I've got now:

Code: Select all

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()

{

   srand(time(0));

   for(int x = 1; x<2;x++) {
       cout << "This is what you rolled:\n" << endl;
       cout << 1+(rand()%20) << endl;


   }

Jivix
Posts: 9
Joined: 03 Feb 2012, 00:41

Post » 20 Feb 2012, 10:33

Hen Barrison wrote:

Code: Select all

    for(int index=0; index<20; index--)
If you don't even understand why you have an infinite loop here, I don't think you should go on with that. Start learning programmation from the beginning (conditions, loops, ...).
Hen Barrison wrote:

Code: Select all

   for(int x = 1; x<2;x++) {
That loop is as stupid as the previous one. The program will enter only one time in the loop, so it is totally useless. Just remove that line.

User avatar
HeroKing
Posts: 104
Joined: 10 Feb 2012, 23:57

Post » 20 Feb 2012, 18:51

Hen Barrison wrote:Well, Now that I have that, I want to prInt text, based on what number the generator gives out.

How would I do that?

Here's what I've got now:

Code: Select all

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()

{

   srand(time(0));

   for(int x = 1; x<2;x++) {
       cout << "This is what you rolled:\n" << endl;
       cout << 1+(rand()%20) << endl;


   }
for one thing, you seem to be missing another } to end your function. and Jivix is right, since that for loop will only work once. i'll show you a header function i wrote for a simple coin toss program i came up with.

Code: Select all

#include "cointoss_h.h"
#include <ctime>
#include <cstdlib>
#include <iostream>
#include "topmenu.h"

using namespace std;

cointoss_h::cointoss_h()
{
    srand(time(0));
    int result=(rand()%100);
    if(result<50){
        cout << "tails" << endl;
    }else{
        cout << "heads" << endl;
    }
}

Post Reply