Thread: how do you make random numbers?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    34

    how do you make random numbers?

    how do you make random numbers in code like rolling dice?

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    tryto make use of srand() and rand() functions

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    whats the difference?

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    srand() allows you to seed the random number generator rand(). If instead you use the default seed, you will get the same sequence of random numbers everytime you run your program. Typically, you seed rand() with the current time--in the form of the number of milliseconds since 1970. You can do that with the time() function: time(0). #include <cstdlib>, <ctime>.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    wow. i learned something new today... thanks a lot!

  7. #7
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    you may also be interested in checking this out: http://www.eternallyconfuzzled.com/tuts/random.html
    Registered Linux User #380033. Be counted: http://counter.li.org

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    so how do you make a the program pick a random number between 1 trough 8 like and cout both of the die?

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Well that's the tricky part. Here's the easiest way:
    Code:
    #include<iostream>
    #include<vector>
    #include<algorithm> //random_shuffle()
    #include<ctime> //time()
    using namespace std;
    
    
    int main()
    {
    	//A vector is just like an array.
            //You declare one by using the word "vector"
            //and putting the type of the elements it will 
            //contain between angled brackets.  Then give it a 
            //name and a size:
    	vector<int> myNums(8);
    	
    	//Put the numbers 1-8 in the vector:
    	for(int i = 0; i < 8; i++)
    	{
    		myNums[i] = i + 1;
    	}
    	
            //Seed the random number generator:
    	srand(time(0)); 
            //Shuffle the vector between the elements specified:
    	random_shuffle(myNums.begin(), myNums.end());
    	//Display the first two values in the shuffled vector:
    	cout<<"You rolled a "<<myNums[0]<<" and a "<<myNums[1]<<endl;
    		
    	return 0;
    }
    Last edited by 7stud; 11-21-2005 at 04:32 AM.

  10. #10
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    pick a random number between 1 trough 8
    why would you want a number 1 through 8? lol the numbers on a die are 1 through 6..?
    Registered Linux User #380033. Be counted: http://counter.li.org

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >lol the numbers on a die are 1 through 6..?
    Clearly the words spoken by someone who hasn't played D&D
    My best code is written with the delete key.

  12. #12
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    Quote Originally Posted by Prelude
    >lol the numbers on a die are 1 through 6..?
    Clearly the words spoken by someone who hasn't played D&D
    truu truu. my mom wouldn't let me even tho my uncles would have loved it. she was basically anti-anything_that_had_magic, lol..
    Registered Linux User #380033. Be counted: http://counter.li.org

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    7stud, your way doesn't match the scenario of rolling two dice. If you have two dice, you are allowed to have both dice give the same result. Using random_shuffle is more appropriate for doing a card game where each card is dealt and you cannot deal duplicate cards.

    To simulate rolling two dice, just use rand() twice. The easiest way to do this is (rand() % 8 + 1), once for each die.

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    7stud, your way doesn't match the scenario of rolling two dice. If you have two dice, you are allowed to have both dice give the same result.
    Good point.

    The easiest way to do this is (rand() % 8 + 1), once for each die.
    That can reduce the randomness because the modulus operator throws out the high order bits and just uses the low order bits. For more information, see Prelude's faq:

    http://faq.cprogramming.com/cgi-bin/...&id=1073086407

    Once you get into all those issues, it gets complicated real fast. I think it's conceptually simpler to modify the code I posted to read a number out of the shuffled vector, then shuffle again and read another number. But, ultimately that's up to findme to decide.
    Last edited by 7stud; 11-21-2005 at 03:50 PM.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't understand how shuffling the entire vector twice would be conceptually simpler than just picking a random number from 1 to 8.

    I doubt the low vs high order bits issue will have much of an impact if you're looking for the easiest method.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random numbers
    By h_howee in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2005, 02:56 PM
  2. Unique random numbers
    By aydin in forum C Programming
    Replies: 7
    Last Post: 11-23-2004, 12:21 PM
  3. Generating Random Numbers
    By FromHolland in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2003, 09:05 AM
  4. 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
  5. programming with random numbers
    By xstudent in forum C Programming
    Replies: 13
    Last Post: 05-21-2002, 01:36 AM