RandomNumberGenerator

This is a discussion on RandomNumberGenerator within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Can someone tell me what is worng with this code?it comes upwith a number but dosent come up with different ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    RandomNumberGenerator

    Can someone tell me what is worng with this code?it comes upwith a number but dosent come up with different numbers
    Im using Dev C++4.0 in windows Thanks

    #include<iostream>
    #include<stdlib.h>
    #include<time.h>
    #include<conio.c>
    void main(){
    int loop,myRandom;
    //randomize
    myRandom=rand()%80;
    cout<<"Random Number"<<myRandom<<endl;
    for(loop=0;loop<100;loop++){
    gotoxy(myRandom,rand()%22);
    cout<<"";
    }
    char myQuit;
    cin>>myQuit;
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    You need to seed the generator. See the FAQ for how to use the time to seed the generator.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    11
    Thanx
    "Yes, I am a criminal. My crime is that
    of curiosity. My crime is that of judging
    people by what they say and think, not
    what they look like. My crime is that of
    outsmarting you, something that you
    will never forgive me for."
    quote from:
    ..+++The Mentor+++

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    11
    i changed the code and it works only 1 problem it dosent stay up for more than a second before it closes! can someone tell me whats wrong#include<iostream>
    #include<stdlib.h>
    #include<time.h>
    #include<conio.c>
    #include <stdio.h>
    void main(){
    {
    srand(time(NULL));
    printf("A random number from 0 to 99: %d", rand() % 100);
    return 0;
    }
    int loop,myRandom;
    //randomize
    myRandom=rand()%80;
    cout<<"Random Number"<<myRandom<<endl;
    for(loop=0;loop<100;loop++){
    gotoxy(myRandom,rand()%22);
    cout<<"";
    }
    char myQuit;
    cin>>myQuit;
    }
    "Yes, I am a criminal. My crime is that
    of curiosity. My crime is that of judging
    people by what they say and think, not
    what they look like. My crime is that of
    outsmarting you, something that you
    will never forgive me for."
    quote from:
    ..+++The Mentor+++

  5. #5
    Disagreeably Disagreeable
    Join Date
    Aug 2001
    Posts
    711
    Your program executes the srand(); and printf(); statements then hits that blasted return 0;. I suspect that's your problem. Return 0;'s belong at the end of the program
    Last edited by Hillbillie; 11-05-2001 at 12:42 PM.

  6. #6
    Disagreeably Disagreeable
    Join Date
    Aug 2001
    Posts
    711

    Red face And another thing...

    Also, it wouldn't hurt to make the main function of type, int.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    11
    THX ill try that =)
    "Yes, I am a criminal. My crime is that
    of curiosity. My crime is that of judging
    people by what they say and think, not
    what they look like. My crime is that of
    outsmarting you, something that you
    will never forgive me for."
    quote from:
    ..+++The Mentor+++

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21