Page 3 of 5

Posted: Sat Sep 04, 2004 12:39 am
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

Posted: Sat Sep 04, 2004 2:17 pm
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

Posted: Sat Sep 04, 2004 2:20 pm
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;
}

Posted: Sat Sep 04, 2004 2:24 pm
by Don Pwnious
So i really dont need it without the program having a loop right?

Posted: Sat Sep 04, 2004 2:30 pm
by JS Lemming
The Phantom wrote:So i really dont need it without the program having a loop right?
Need what?

Posted: Sat Sep 04, 2004 2:49 pm
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

Posted: Sat Sep 04, 2004 6:58 pm
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?

Posted: Sun Sep 05, 2004 7:59 am
by JS Lemming
Anyone I suppose, but I don't know the answer. I highly doubt it though.

Posted: Sun Sep 05, 2004 8:39 am
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()!

Posted: Sun Sep 05, 2004 12:23 pm
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. :)

Posted: Sun Sep 05, 2004 4:57 pm
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

Posted: Sun Sep 05, 2004 5:13 pm
by Falco Girgis
Yeah, I didn't think that my own friend would sink that low...

Posted: Sun Sep 05, 2004 5:16 pm
by Don Pwnious
Of course i wouldnt sink that low

Posted: Mon Sep 06, 2004 8:35 am
by Don Pwnious
Now im on loops.

Which is better the while or do statemant?

Posted: Mon Sep 06, 2004 9:13 am
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;
}