random number per milisecond

This is a discussion on random number per milisecond within the C++ Programming forums, part of the General Programming Boards category; I've looked at this post http://cboard.cprogramming.com/showthread.php?t=53242 and I checked preludes random number generator, but can't you make it shift to ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    20

    random number per milisecond

    I've looked at this post
    Random Number

    and I checked preludes random number generator, but can't you make it shift to a new number every half or whole milisecond?

    It would be better since it is, well, really slow for a game of dice.
    Last edited by Xarr; 06-05-2004 at 11:00 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    Yeah, stop calling
    srand( time(0) );
    each time you want a new rand() value

    You only need to call this ONCE per program run.
    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
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    every call to rand() will give you another number in the sequence. The time is only a factor when seeding the generator.

    Edit: Dang beaten

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    20
    Oh yeah almost forgot

    How would I put the random number into a variable instead of outputting it?

    The class std isn't defined in the code...

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    std is the namespace.

    to put it in a variable you would just use it like any other return value from a function.

    int x = rand();

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    20
    but it isn't the rand() function i'm talking about it's the one prelude wrote. The 5th reply here: Random Number

    there (edit: she) defines the implementation thing and the fucntion random().

    What I would like to know is (apart from how to change it's shifting speed): how to save the number that the for loop makes into an variable "x" instead of cout'ing it
    Last edited by Xarr; 06-05-2004 at 02:23 PM.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Its still a function. It follows the exact same rules as any other return value.

    BTW Prelude is a she.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
      int x;
      std::srand(time_seed());
      for (int i = 0; i < 10; i++) {
        x = static_cast<int>(random() * 10);
    For dice, you'd use:
    Code:
      int x;
      std::srand(time_seed());
      for (int i = 0; i < 10; i++) {
        x = static_cast<int>(random() * 6);
    This will give an int between 0 and 5, representing one die.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >This will give an int between 0 and 5, representing one die.
    A six sided die is the most common, but certainly not the only kind available. If I were to write a simple die thrower then I would include the number of sides as an argument:
    Code:
    class diemension_error: public invalid_argument {
    public:
      diemension_error(const string& msg)
        : invalid_argument(msg)
      {}
    };
    
    int
    die_toss(
      int sides
      )
    {
      if (sides < 4) {
        throw diemension_error("3-D die only please"); // ;-)
      }
    
      return static_cast<int>(random() * sides);
    }
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >class diemension_error: public invalid_argument {

    DIEmension ... that's a good one Prelude, haha.

    That's some neat code. I'm going to try running it. Is invalid_argument in the std library?

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is invalid_argument in the std library?
    It is indeed. Be sure to include <stdexcept> though.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 09:46 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. How exactly does the random number generator work?
    By Finchie_88 in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 12:46 AM
  4. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 07:40 AM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM

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