Thread: how do you make random numbers?

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >I don't understand how shuffling the entire vector twice would
    >be conceptually simpler than just picking a random number from 1 to 8.
    Conceptually, it's probably easier to see how shuffling a vector results in a suitably random value while shifting the range of a random number with the remainder operator isn't immediately obvious unless you're a math major. On the other hand, the former is less likely to be used if all you need is a quick and dirty roll of the dice.

    >I doubt the low vs high order bits issue will have much of an
    >impact if you're looking for the easiest method.
    Or at all, since these days compiler vendors have figured out that their implementations of rand sucked ass. The low order bits problem isn't as bad as it used to be, but you still have to contend with the distribution problem of shrinking a range that was fragile to begin with.

    >For more information, see Prelude's faq:
    Prelude's other FAQ.
    My best code is written with the delete key.

  2. #17
    Registered User
    Join Date
    Nov 2005
    yawn i got it.
    //adds the iostream library to the program
    #include <iostream>
    #include <cstdlib>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define BOOL_H
    #undef true
    #undef false 
    //informs the compiler you are using the standard library set
    using namespace std;
    const int LOW = 1;
    const int HIGH = 6;
    int firstDie;
    int secondDie;
    int Choice;
    int yes;
    Variables to hold random values
    for the first and the second die on
    each roll.
    int main ()    
       time_t seconds;
       srand((unsigned int) seconds);
       for ( ;; )
          cout << "Round 1 \n";
          cout << "Would you like to roll this round? [1 for yes, 0 for no]:\n\n";
       if ( cin >> yes || yes == 0 )
          {firstDie = rand() % (HIGH - LOW + 1) + LOW;
    (secondDie = rand() % (HIGH - LOW) + 1) + LOW;
    cout << "you rolled a "<< firstDie << " and a " << secondDie << endl; break;}    
       return 0;

  3. #18
    Registered User
    Join Date
    Apr 2003
    Well, well, well. yawn right back at you.

    >For more information, see Prelude's faq:
    Prelude's other FAQ.
    Arghhh. Now I have to worry about srand() too?

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