473,385 Members | 1,324 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,385 software developers and data experts.

Adding Random Numbers

I'm having trouble adding random numbers that I've generated using a for loop. So far my block of code looks:

cout<<"Please enter the number of dice you want to use:";
cin>>numberOFdice;
for(x=0; x<numberOFdice; x++)
cout<<(rand()%6)+1<< endl;


Now I need to add the random numbers that were generated. How do I do that?
Jul 18 '06 #1
1 3012
Banfa
9,065 Expert Mod 8TB
Following the way you have used rand()

Expand|Select|Wrap|Line Numbers
  1. int value;
  2. int total = 0;
  3. cout<<"Please enter the number of dice you want to use:";
  4. cin>>numberOFdice;
  5. for(x=0; x<numberOFdice; x++)
  6. {
  7.     value =(rand()%6)+1; 
  8.     cout<<value<< endl;
  9.     total += value;
  10. }
  11.  
  12. cout<<total<< endl;
  13.  
However quite often the low order bits of the value returned by rand() are not all that random and rand()%6 discards all but the low order bits so you may not be getting a very random sequence, see

http://c-faq.com/lib/randrange.html
Jul 19 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

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...
1
by: bob | last post by:
I am a newbie. How do I add an array to a viewstate, then retreive the individual elements? Right now, I made an array with random numbers and when I go to access an element from different...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
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??...
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,...
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
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 ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.