My code wont generate randon numbers

This is a discussion on My code wont generate randon numbers within the C++ Programming forums, part of the General Programming Boards category; please help me my program is done all i need is to make random numbers, but for some reason i ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    Question My code wont generate random numbers

    please help me my program is done all i need is to make random numbers, but for some reason i keep on getting the same number over and over heres the code for a part of this program

    Code:
    #include <iostream>
    #include <iomanip>
    #include <ctime>
    using namespace std;
    
    
    int main(void)
    {
    	double dPot=100.0;   //Starting Pot Size
    	double dBet=0.0;     //Bet that user inputs  
    	int iHand=0;        //Addidtion of users cards
    	char cAns;         //answer either y/n
    	srand((unsigned)time(NULL));
    	unsigned int uiHand=0,uiCurrentCard=0,uiDealer=16;
    	  uiCurrentCard= rand()%10;
    	  
    	  //users card
    	cout <<"\t\t\t\tWelcome to 21!\n\n\n";//title 
    		
    	//getting users Bet
    	
    	do{
    		cout<<"You have " <<dPot<< " dollars to bet with\n\n";
    		cout<<"How much would you like to bet? ";
    		cin>>dBet;
    
    		//make sure its a vaild bet and pass all tests
    		 while((dBet<=0)||(dBet>dPot)||(cin.fail()))
    		 {
    			cin.clear();
    			cin.ignore(cin.rdbuf()->in_avail(),'\n');
    			cout<<"Sorry that was an invaild bet.\n";
    			cout<<"How much would you like to bet? ";
    			cin>>dBet;
    		 }
    			//give Player first Card Ask him if he wants another
    so when i cout uiCurrentCard i always get the same number thro the program why is that?
    Last edited by n00b_101; 10-20-2005 at 01:07 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    > uiCurrentCard= rand()%10;
    Put this INSIDE your loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    thanks for your reply, i tried that and that and it didnt work, got all 7sevens.

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Well try reading the FAQ on this site about random numbers perhaps?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    You should post your new code - there's no way of telling what sort of creative way you could have made a mess of it (we've seen plenty).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM
  5. who can sent me code about 8 numbers
    By xacoolboy in forum C Programming
    Replies: 7
    Last Post: 03-08-2002, 06:33 PM

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