Just starting up

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post by MarauderIIC »

You have to re-seed every time or you get the same random number.

Code: Select all

unsigned int rnum;

srand(time(NULL));

rnum = rand();

srand(rnum);

rnum = rand();
// etc
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

You have to re-seed every time or you get the same random

Code: Select all

number.unsigned int rnum; 

srand(time(NULL)); 

rnum = rand(); 

srand(rnum); 

rnum = rand(); 
// etc 
Well ok where do i put it?

Code: Select all

#include <iostream>
#include <time.h>
using namespace std;
	
int main()
{
    //Seed the random generator
    srand( (unsigned)time( NULL ) );
    
    int number = rand() % 100;
    int guess;

    cout << "I am thinking of a number between 1 and 100" << endl;
    cout << "Enter your guess, please ";
    cin >> guess;
    if (guess == number)
    {
        cout << "Incredible, you are correct" << endl;
    }
   else
    {
        cout << "HA HA, YOU SUCK!!!" << endl;
    }

    return 0;
}  
After i finish with this random thing i gonna make a "loop
Last edited by Don Pwnious on Sat Sep 04, 2004 2:17 pm, edited 2 times in total.
1/8th time- 14secs
1/8th speed - 110mph
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

I think the best thing to do is to make a function for random numbers. Try this:

Code: Select all

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std; 

//Function: RandInt()
//Parameters: min, max
//Description: Returns a random int between a min and max
int RandInt(int min, int max)
{
    //Seed the random generator
    srand( (unsigned)time( NULL ) );

    //Generate and Return an interger between min and max
    return (rand() % max) + min;
}

//Nothing imaportant
char Blaa;

int main(int argc, char *argv[])
{
    //Display a random number
    cout << "Woot Woot, a random number: " << RandInt(0,100);

    //Keeps the console box from closing to soon
    cin >> Blaa;

    return 0;
}
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

So i really dont need it without the program having a loop right?
1/8th time- 14secs
1/8th speed - 110mph
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

The Phantom wrote:So i really dont need it without the program having a loop right?
Need what?
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post by MarauderIIC »

Except the best way to guarantee a random number is to seed with your last random number. :)

And you don't have a loop here :P

AHA! Okay, I got you. No, you don't need to reseed unless you loop.

You would do something like this, I suppose -
and what sucks is that i can't format text inside code brackets. i'll have to see about changing that somewhere... sometime.

Code: Select all

int main()
{
    //Seed the random generator with time
    srand( time( NULL ) );

    int number;
    int rawNumber = rand(); //generate a random number
    int guess;

    while (/*some condition*/) {     
        //these lines here generate a new number every time the loop
        //goes through an iteration. (ooh big word look out)
        srand(rawNumber); //this seeds with your last random number
        rawNumber = rand();
        number = rawNumber % 100;

        cout << "I am thinking of a number between 1 and 100" << endl;
        cout << "Enter your guess, please ";
        cin >> guess;
        if (guess == number)
        {
            cout << "Incredible, you are correct" << endl;
            break; //leave the loop
        }
        else
        {
            cout << "HA HA, YOU SUCK!!!" << endl
                 << "NEW NUMBER!!! BWAAHAHAHAHA" << endl;
        }
    } //close the while loop

    return 0;
} 
Can rand() return values < 0? :P
Last edited by MarauderIIC on Sat Sep 04, 2004 2:49 pm, edited 1 time in total.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

AHHHHHH!!!!!!!!!!!! I didnt want to do the loop yet, oh well thanks for the effort and the time, i will refer to it when i will do the loop
Can rand() return values < 0? :P

Who are you talking to?
1/8th time- 14secs
1/8th speed - 110mph
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

Anyone I suppose, but I don't know the answer. I highly doubt it though.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

Lord, when did Phantom start doing this?

Code: Select all

Something
{
    Good lord...
}
Instead of:

Code: Select all

Something {
    Weee! I'm happy and efficient, not to mention I'm beautiful!
}
Also, I'd just like to say: OMFG! t3h power of Rand()!
Last edited by Falco Girgis on Sun Sep 05, 2004 8:39 am, edited 1 time in total.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post by MarauderIIC »

Looked it up. Between zero and RAND_MAX, which is a predefined constant. In MSVC++ libraries, it's 0x7fff which mr. calculator says is 32,767 which I believe but I keep coming up 5,000 short. :)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

Super Sonic wrote:Lord, when did Phantom start doing this?

Code: Select all

Something
{
    Good lord...
}
Instead of:

Code: Select all

Something {
    Weee! I'm happy and efficient, not to mention I'm beautiful!
}
Also, I'd just like to say: OMFG! t3h power of Rand()!

WHOOPS IT WAS MISTYPED WHEN I EDITED IT
1/8th time- 14secs
1/8th speed - 110mph
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

Yeah, I didn't think that my own friend would sink that low...
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

Of course i wouldnt sink that low
Last edited by Don Pwnious on Sun Sep 05, 2004 5:16 pm, edited 1 time in total.
1/8th time- 14secs
1/8th speed - 110mph
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

Now im on loops.

Which is better the while or do statemant?
1/8th time- 14secs
1/8th speed - 110mph
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

New version of random numbers function.

Code: Select all

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std; 

//Function: RandInt()
//Parameters: min, max
//Description: Returns a random int between a min and max
int RandInt(int min, int max)
{
    //Seed the random generator with the last random number
    srand( rand() );

    //Generate and Return an interger between min and max
    return (rand() % (max-min+1)) + min;
}

//Nothing imaportant
char Blaa;

int main(int argc, char *argv[])
{
    //Seed the random generator with time (only time you have to do so)
    srand( time( NULL ) );

    //Display a random number
    cout << "Woot Woot, a random number: " << RandInt(10,20);

    //Keeps the console box from closing to soon
    cin >> Blaa;

    return 0;
}
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
Locked