gfxHomeForumHelpLoginRegistergfx
gfxgfx
      « previous next »
Pages: [1] Go Down Print
Author Topic: C++ question  (Read 2738 times)
Silentknife
Guest
« on: October 25, 2007, 05:41:56 AM »

Im having a problem in my beginning c++ class.  The problem is that when I use the srand(time(0)) function it results the first number always being the same in a random number generating hi-lo guessing game.  That magic number is 42.  All following numbers generally work out pretty random in a test of 100 tries.  If this is just the result of me just learning basics ok, ill learn better later or if its something wrong, any info would be appreciated.
Logged
Jim Tressel
[global] WTB new character name
Hero Member
*****

Karma: +78/-14
Offline Offline

Posts: 4220



« Reply #1 on: October 25, 2007, 06:08:35 AM »

Not sure myself.  My coding is a bit (lot) rusty.  I did find this though, hope it helps:

http://www.daniweb.com/forums/thread80036.html
Logged


Silentknife
Guest
« Reply #2 on: October 25, 2007, 06:34:02 AM »

Thanks man, it looks like that my code is just noobish as I haven't started delving into some of the concepts in the code.
Logged
Krogoth
Has a PS2 Hardon
WoW Member
Hero Member
***

Karma: +6/-0
Offline Offline

Posts: 709


« Reply #3 on: October 25, 2007, 02:14:38 PM »

try this instead of just srand(time(0)):

Code:
srand( static_cast<int> (time(0)) );

not sure the cause, though. Put the code up as an attachment, I'll take a look at it, see whats up.
Logged

Griba
Guest
« Reply #4 on: October 25, 2007, 04:23:47 PM »

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main( void )
{
   int i;

   /* Seed the random-number generator with current time so that
    * the numbers will be different every time we run.
    */
   srand( (unsigned)time( NULL ) );

   /* Display 10 numbers. */
   for( i = 0;   i < 10;i++ )
      printf( "  %6d\n", rand() );
}


this is the example from MS
Logged
Silentknife
Guest
« Reply #5 on: October 25, 2007, 07:45:51 PM »

#include <iostream>
#include <ctime>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;


int main ()
{
 
  string begin = "";
 
  do
  { 
  double guess = 0.0;
  int actual = 1 + rand() % (100 + 1 - 1);
  int attempts = 0;
 
  //random number generator
 
 
  {
 
 
  do
  {
  cout << "Please enter your guess: ";
  cin >> guess;
  attempts = ++attempts;
   
     {
     if (guess > actual && guess <= 100)
             
              cout << "Too High! Try again!" << endl << " " << endl;
 
     if (guess < actual && guess >= 1)
              cout << "Too Low! Try again!" << endl << " " << endl;
             
     if (guess > 100)
              cout << "ERROR!: The number you entered, " << guess << ", is greater than 100." << endl << "The number must be between 1 and 100." << endl << " " << endl;
     
     if (guess < 1)
              cout << "ERROR!: The number you entered, " << guess << ", is less than 1." << endl << "The number must be between 1 and 100." << endl << " " << endl;
     
     if (guess == actual)
              cout << "Exact guess! It took you " << attempts << " attempt(s) to guess the correct answer of " << actual << "." << endl << " " << endl;
     }
   
  } while(guess != actual);
  }
   cin.ignore(100, '\n');
   cout << "Would you like to play again? (y/n)";
   getline(cin, begin);
   
   if (begin == "n")
   {
    cout << " Thank you for playing." << endl;
   }           
}while (begin != "n");
 
   
         
     
      system("PAUSE");
return 0;   
 
}//end main




This is the main body of the program, It only gives 42 on the first try when you first begin the program.
Logged
Krogoth
Has a PS2 Hardon
WoW Member
Hero Member
***

Karma: +6/-0
Offline Offline

Posts: 709


« Reply #6 on: October 26, 2007, 04:11:21 AM »

yea, i see whats going on.

The rand() fucntion doesnt actually create a totally random number, it actually takes a number and applies a freaky equation to it to form the next number, its just so out there it *seems* random. If you dont set a seed for rand() by using either time or a user-entered seed, it will always have the same numbers in the same order every time, since its taking the same muber and applying the same formula to it.

I'm betting your professor is trying to get you to learn that through trial and error, thats what ours did (he only had us write a program in class, and showed us what was going on).

Your best bet is to, for now, just use time(0) in your rand() function. You can also have your user set it manually if you want. This would be the code:

Code:
    int seed;       //the seed for the random number generator
    cout << "Enter the seed for the random number generator: ";
    cin >> seed;
   
    srand(seed);

srand(int)  is the function to set the seed for the random number (seed of random = srand)

hope that helps


edit: oh, and something to make your coding a little neater in the headers, instead of all the "using std::whatever" try "using namespace std;"
that will include every std in each library, getting rid of a few unneeded lines of code

« Last Edit: October 26, 2007, 04:16:04 AM by Krogoth » Logged

Silentknife
Guest
« Reply #7 on: October 26, 2007, 12:50:42 PM »

My problem was actually a little simple.  Since this is an online course I dont have the benefit of a professor teaching me anything really, hehe.  My problem was that I had moved some code around so that when the loop went back to the beginning that it would reset the values.  In doing this I had move the srand() function to After the rand() function.  It seems to have been fixed.  Also I can just get rid of


using std::cout;
using std::cin;
using std::endl;
using std::string;

and use

using namespace std;

?
Logged
Krogoth
Has a PS2 Hardon
WoW Member
Hero Member
***

Karma: +6/-0
Offline Offline

Posts: 709


« Reply #8 on: October 26, 2007, 03:44:43 PM »

yep. "using namespace std" is the same as writing every "using std::**" value for each library.
Logged

jugs
Guest
« Reply #9 on: October 26, 2007, 05:16:08 PM »

yea, and the flux capacitors don't return the NULL to the ::"" SRAND(01)

Niner, GOTO Line 2 and then take the int(0) {} to the N

if you need anymore assistance, I'm $100 / hr. Thanks....
Logged
Griba
Guest
« Reply #10 on: October 27, 2007, 01:02:48 AM »

This is why I did all your C++ assignments for you Liam.  Or was it Pascal?
Logged
gfx
Pages: [1] Go Up Print 
gfx
Jump to:  
gfx
Powered by SMF 1.1.21 | SMF © 2015, Simple Machines
Helios / TinyPortal v0.9.8 © Bloc
gfx
Powered by MySQL Powered by PHP Valid XHTML 1.0! Valid CSS!
fmclip.com