how do you make random numbers in code like rolling dice?
Printable View
how do you make random numbers in code like rolling dice?
tryto make use of srand() and rand() functions
whats the difference?
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>.
wow. i learned something new today... thanks a lot!
you may also be interested in checking this out: http://www.eternallyconfuzzled.com/tuts/random.html
so how do you make a the program pick a random number between 1 trough 8 like and cout both of the die?
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;
}
why would you want a number 1 through 8? lol the numbers on a die are 1 through 6..?Quote:
pick a random number between 1 trough 8
>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..Quote:
Originally Posted by Prelude
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.
Good point.Quote:
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.
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:Quote:
The easiest way to do this is (rand() % 8 + 1), once for each die.
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.
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.
>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. ;)
yawn i got it.
Code://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;
time(&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 )
{}
else
{firstDie = rand() % (HIGH - LOW + 1) + LOW;
(secondDie = rand() % (HIGH - LOW) + 1) + LOW;
cout << "you rolled a "<< firstDie << " and a " << secondDie << endl; break;}
}
system("pause\n\n\n");
return 0;
}
Well, well, well. yawn right back at you.Quote:
yawn
Arghhh. Now I have to worry about srand() too? :(Quote:
>For more information, see Prelude's faq:
Prelude's other FAQ.