473,394 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Random numbers as objects

Hello,

I have created a class called Die and an implementaion of Die (ex. to
follow) where the default generates a NEW digit 1-6.

I am creating a dice game wherein: a die is rolled (Roll_ 1) then the user
is asked to guess if Roll_2 will be higher or lower than Roll_1.

Without showing the rest of this riduclous project, my issue is that the new
objects Roll_1 & Roll_2 are the same number in the comparison.

However, when I go through the game a second time (the user can play again),
a different number is generated for Roll_1, yet again Roll_2 is the same.

Implemenation code:
Die::Die() {
int roll;
srand((unsigned int)time(0));
do {
roll = 1 + rand()%6;
}while (roll <= 0);
Set_FaceUp(roll);
}

Driver Code:
Die Roll_1;
Die Roll_2;

Any advice would be appreciated, I'm stuck! Thanks
Jul 22 '05 #1
2 1098
On Sat, 21 Feb 2004 21:02:03 GMT in comp.lang.c++,
<mi******@sbcglobal.net> was alleged to have written:
Die::Die() {
int roll;
srand((unsigned int)time(0));


You should never be calling srand() more than once per execution of your
program. Put the srand() call at the beginning of main() and omit it
elsewhere.

The usual answer to that is the same in C++ as it is in C, and is
covered in Steve Summit's C FAQ. It is always good to check the FAQ
before posting. You can get the FAQ at:
http://www.eskimo.com/~scs/C-faq/top.html

For a better random number generator see http://www.boost.org or
Stroustrup chapter 22.7.
Jul 22 '05 #2
Thank you, thank you - I was not able to move the duplicitous srand, but I
was able to follow the links where I found better code for random numbers.

(FYI - I used (int)((double)rand() / ((double)RAND_MAX + 1) * N) - where n
is the max number (6))
"David Harmon" <so****@netcom.com> wrote in message
news:41****************@news.west.earthlink.net...
On Sat, 21 Feb 2004 21:02:03 GMT in comp.lang.c++,
<mi******@sbcglobal.net> was alleged to have written:
Die::Die() {
int roll;
srand((unsigned int)time(0));


You should never be calling srand() more than once per execution of your
program. Put the srand() call at the beginning of main() and omit it
elsewhere.

The usual answer to that is the same in C++ as it is in C, and is
covered in Steve Summit's C FAQ. It is always good to check the FAQ
before posting. You can get the FAQ at:
http://www.eskimo.com/~scs/C-faq/top.html

For a better random number generator see http://www.boost.org or
Stroustrup chapter 22.7.

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bart Nessux | last post by:
New to Python... trying to figure out how to count the objects in a list and then map the count to the objects or convert the list to a dict... I think the latter would be better as I need a number...
3
by: Heath | last post by:
Using MSIE 5+ Heath writes: My problem deals with working with window objects between pages as follows: My Introduction page contains a link to my Action page. The onClick of that link...
21
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to...
3
by: clsmith66 | last post by:
I'm hoping someone can help me with a problem I'm having using the random class. I need to return a "random" number between 0 and 15, so i did this Dim r as new Random() Return r.Next(0, 15) ...
5
by: Muffin | last post by:
I hope somebody can show me why I need to have a messagebox to get "random" numbers in my example. If I comment out the message box call that is in RollAbility() the numbers generated are not...
12
by: Naya | last post by:
Hi. I am working on a math tutoring program which generates two random numbers (from 1 to 500) and asks the user to add them. How can I check to see whether or not they put in the correct total??...
41
by: Zytan | last post by:
Anyone do any tests on it? I would assume it has improved since C's rand(), but who knows. For some reason, and it could just be coincidence, I seem to see patterns. But even a crappy rand()...
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
16
by: raylopez99 | last post by:
For the public record. RL public void IterateOne() { Random myRandom = new Random(); //declare Random outside the iteration for (int j = 0; j < Max; j++) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.