Thread: random numbers

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    12

    random numbers

    i am using srand and rand to generate several random numbers. after i finally figured out that i was calling srand every time that i generated one. i moved it and now it generates different numbers. the numbers i am generating are between 1 and 66992 but after running it several times, i have had no numbers over 32347 generated. anyone know why this is? as i am reading from an alphabetical list, i am not getting words from about g down.

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Just bad luck lol

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Code:
    std::cout << RAND_MAX;
    Soma

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    RAND_MAX came back as 32767.....huh?

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Ok so here is how I do it
    As soon as I start my program I use

    Code:
    srand(GetTickCount());
    And then when I want random I use

    Code:
    int q = rand()%range;
    It works for my on windows, but gives me an error on my ubuntu.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    RAND_MAX came back as 32767.....huh?
    The macro `RAND_MAX' is provided to "tell" the program/programmer the highest value `std::rand' can naturally generate. You can use an outside implementation or "reseat" the range.

    It works for my on windows, but gives me an error on my ubuntu.
    The `GetTickCount' function is a Windows API.

    Soma

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    so how can i do that. here is the code. i have some extra couts in there for testing purposes. keep in mine i am kinda new to c++.
    Code:
    string randomWord(){
    		char retry = ' ';
    		int lineCount = 0;
    		int randomWord_int = 0;
    		string line = "";
    		vector<string>randomWord_vect;
    		do
    		{
    			ifstream wordFile ("regex.txt");
    
    			if (wordFile.is_open())
    			{
    				while (!wordFile.eof())
    				{
    					getline(wordFile, line);
    					if (line != " ")
    					{
    						randomWord_vect.push_back(line);
    						lineCount++;
    					}
    				}
    				int x = 0;
    				for (int i = 0; i < 100; i++){
    					randomWord_int = rand() % lineCount -1;
    					if (randomWord_int > x){
    						x = randomWord_int;
    					}
    				}
    				cout << "The highest was: " << randomWord_int << endl;
    				cout << "The Max is: " << RAND_MAX << endl;
    				randomWord_int = rand() % lineCount -1;
    				wordFile.close();
    				break;
    			}
    			else
    			{
    				cout << "Error. Unable to open file." << endl;
    				cout << "Try again? [Y]es / [N]o" << endl;
    				cin >> retry;
    			}
    
    		}while (retry == 'Y' || retry == 'y');
    		return randomWord_vect[randomWord_int];
    	}

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    well i found a cheap way to get more numbers out of it.

    Code:
    randomWord_int = rand()+rand() % lineCount -1
    it just doubles its so now i am getting numbers up in 50000 range.

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You did "reseat" the range. I'll give you that.

    Note: I've omitted range/error checking.

    Soma

    Code:
    int random(int min, int max)
    {
    	using namespace std;
    	return(min + ((rand() * (1.0 / (RAND_MAX))) * (max - min)));
    }

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    thanks. i got it now. i have another question im not even sure you can do in c++.
    i want to do the same task to several different variables with as little code as possible. is there a way to do something like....

    Code:
    var1 = 12;
    var2 = 16;
    var3 = 9;
    for (int i = 0; i <= 3; i++){
         var[i] +=1
    }
    this is just how i would think something like this would work. this will not compile.

  11. #11
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    Because var is not a symbol in that snippet. var1, var2 and var3 are. If you want to do that you can do something like:
    Code:
    vars[] = {12, 16, 9};
    for(int i = 0; i <= 3; ++i)
        vars[i] += 1;
    -- Will you show me how to c++?

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    i cant seem to get it to work like that. maybe because my variables are not of type int. before was a bad example i guess. here is a piece of my code.

    Code:
    if (curWord1 == ""){	//puts *'s in place of all characters in string user is trying to guess
    	for (int i = 0; i <= 3; i++){
    		for (ix = xword[i].begin(); ix < xword[i].end(); ix++){
    		curWord[i] += "*";
    it makes a string of *'s the same size as the random word. but there is 3 words i need to do that to. should i just copy and paste the code and change the variables. im just trying to make as little code as possible

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with displaying random numbers
    By Bumps in forum C++ Programming
    Replies: 12
    Last Post: 10-03-2009, 12:29 PM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  5. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM

Tags for this Thread