Random Number Generator

This is a discussion on Random Number Generator within the C++ Programming forums, part of the General Programming Boards category; Hey i have started this random number generator and i am having a problem. The code produces a random integer ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    17

    Random Number Generator

    Hey i have started this random number generator and i am having a problem. The code produces a random integer from 0 to 4. This is good, but i was wondering how i could alter the code to get a random number from 1 to 4. Thats it
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int argc, char *argv[])
    {
      time_t now;
      time(&now);
      srand(now);
      rand();
      for (int i=0; i<1; i++) {
      cout << rand() % 5 << endl;
      }
      system("PAUSE");	
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    The easiest way to remember this is:
    Code:
    rand()%(high - low + 1) + low
    Where high is the highest random number, and low is the lowest random number. So to get a range from 1 - 4:

    Code:
    rand()%(4 - 1 + 1) + 1
    Or to shorten it:
    Code:
    rand()%4 + 1
    Last edited by funkydude9; 08-16-2003 at 10:54 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    17
    I tried your suggestion, and while it does accomplish the goal of excluding 0 it does something else also. I ran the program multiple times with your suggestion and the re sults i got were something like this:
    1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4,1, 1,1, 2, 2, ect.
    Basically what i am trying to say is that it doesnt look to random to me. It looks like it is just making some kind of sequence towards 4 and then starting over again at 1.

  4. #4
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    try giving it a larger amount of space like:
    rand()%20+1;

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Remember, your computer's random number generator isn't perfect. That's why it's called a pseudo-random number generator. What you can do is generate a random number between zero and rand_max. Then, if the number is in the first fourth of that range, return 1, and so on. That might yield seemingly more random results.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    17
    Ok Ok so if i give it a larger range to work with, i get what appear to be more randomized numbers. But i guess what i should say is that my goal is eventually to make a dnd style text adventure. And i need some kind of random number generator to act as a 4 sided die. Apparently the suggestions made earlier in this thread work well for numbers greater than 4 but for a 4 sided die are there any suggestions????


    Am i being to picky? I mean will having the number almost sequenced like they are with the 1-4 generator really affect the gameplay? The last thing i want is you damage roll to increase untill it gets to 4 and then drop back down to one.

  7. #7
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Originally posted by Ikurik
    I tried your suggestion, and while it does accomplish the goal of excluding 0 it does something else also. I ran the program multiple times with your suggestion and the re sults i got were something like this:
    1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4,1, 1,1, 2, 2, ect.
    Basically what i am trying to say is that it doesnt look to random to me. It looks like it is just making some kind of sequence towards 4 and then starting over again at 1.
    Bad luck, I guess.
    Try making this
    Code:
    for (int i=0; i<1; i++) {
      cout << rand() % 5 << endl;
    }
    into this:
    Code:
    for (int i=0;i < 10; ++i) {
      cout << rand()%4 + 1 << endl;
    }
    So it prints out more often per program execution.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    17
    I just had an idea but im not sure how to implement it. Since the numbers seem to be more random as the range increases. Could i possibly make a random number from 1 to 8, then divide that number in half and then round that number to the nearest whole? And if that is possible i really have no clue about how to go about it.


    thanks

  9. #9
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    i = rand()%7;
    i++;
    i = i / 2;

    afaik you need to force the program to give decimal points

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One idea:
    Code:
    cout << (int)((double) rand() * 4. / RAND_MAX + 1.) << endl;

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Try this:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
    	srand(time(NULL));
    
    	int randnum;
    
    	for (int i = 0; i < 50; i++)
    	{
    
    		randnum = rand()%(100);
    
    		if (randnum <= 25)
    		  randnum = 1;
    
    		else if (randnum <= 50)
    		  randnum = 2;
    
    		else if (randnum <= 75)
    		 randnum = 3;
    
    		else
    		  randnum = 4;
    
    		cout<<randnum<<endl;
    	}
    
    
      return 0;
    }
    Actaually that might make it worse...lol.

    Put this line of code in the beginning of main(), as it seeds the random number generator:
    Code:
    srand(time(NULL));
    Also, make sure to store your random number in a variable. ie don't do this:
    Code:
    if (rand()%5 == 0)
    {
        //do stuff
    }
    else if (rand()%5 == 1)
    {
       //do stuff
    }
    else if...
    It won't be as accurate because you're actually making a new random number everytime in your if statement.
    Last edited by funkydude9; 08-16-2003 at 11:56 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Most of those solutions just try to cover up the problem without actually dealing with it. In fact, typically the low-order bits are the most "random". Swoopy's solution is probably the best, however.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    17
    Thanks you guys for your suggestions. Hopefully i can use them to make a cool game somewhere down the line.
    Once again thanks for the help.

  14. #14
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    This is good, but i was wondering how i could alter the code to get a random number from 1 to 4. Thats it
    This solution should be sufficient for you (no duplicates).


    Code:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
    
    	time_t now;
    	time(&now);
    	srand(now);
    
    
    		/* Creates 'random' numbers between 1 and higestNumber*/
    		/* Note: Program crashes if howmanyNumbers > higestNumber */
    		int howmanyNumbers = 1;
    		int highestNumber = 2;
    		int i;
    
    		//Fill temp array with all possible numbers
    		int *temp = new int[highestNumber];
    		for (i = 0; i < highestNumber; ++i)
    			temp[i] = i+1;
    
    		//How many random numbers do you need?
    		int *randomNumbers = new int[howmanyNumbers];
    
    		for (i = 0; i < howmanyNumbers; ++i)
    		{
    			int random = rand() % highestNumber;
    
    			randomNumbers[i] = temp[random];
    
    			temp[random] = temp[highestNumber-1];
    			highestNumber--;
    		}
    
    		for (i = 0; i < howmanyNumbers; ++i)
    			cout << randomNumbers[i] << endl;
    
    		delete[] temp;
    		delete[] randomNumbers;
    
    return 0;
    }
    This produces quite random numbers.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  15. #15
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by Zach L.
    Most of those solutions just try to cover up the problem without actually dealing with it. In fact, typically the low-order bits are the most "random". Swoopy's solution is probably the best, however.
    Zach, Stoustrup says exactly the opposite in The C++ Programming Language, 22.7. He recommends another method:
    int( (double(rand()) / RAND_MAX) * n)

    for numbers from 0 to n-1. Note this does have a miniscule but nonzero chance (0.00000005% for 31 bits of randomness) of returning n itself, so a serious application would have to take this into account.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  2. Good Random Number Generator
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 11-18-2004, 05:38 AM
  3. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  4. how to link random number generator with cpu?
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2003, 05:26 AM
  5. Seeding Random Number Generator
    By zdude in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2002, 03:10 PM

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