C++ :: Random Number Generator - Count How Many Times Each Number Shows Up
Sep 26, 2012
I'm running a game online and designing a program to generate Enemy Stats. Basically, it's supposed to generate 25 numbers between 0 and 7(to represent 8 Attributes on a 25 Point Buy system) and count how many times each number shows up.
Here's what the code looks like:
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int Generate() {
int r= rand();
int s= r%7;
[Code] ....
And here's two outputs.
1: Code:
12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 12133089220 Stats[0]= 25 Stats[1]= 0 Stats[2]= 0 Stats[3]= 0 Stats[4]= 0 Stats[5]= 0 Stats[6]= 0 Stats[7]= 0 Stats[8]= 0 Disallowed system call: SYS_socketcall
2: Code:
14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 14787708434 Stats[0]= 0 Stats[1]= 0 Stats[2]= 0 Stats[3]= 0 Stats[4]= 25 Stats[5]= 0 Stats[6]= 0 Stats[7]= 0 Stats[8]= 0 Disallowed system call: SYS_socketcall
Note that the number does change, but only between runs.
View 3 Replies
ADVERTISEMENT
Feb 6, 2014
In this program what i'm doing is to search for a number when the vector is in order, and count how many times that number appears, the problem is that count how many times the number does not appear, but when its appear the program stays in an "standby mode" and the cursor does not move.
int buscarNumOrdenado (int x [MAX]) //MAX is the define {
int num,d, LimiteInferior, LimiteSuperior,mitad;
d=0;
LimiteInferior = 0;
LimiteSuperior = MAX-1;
[Code] ....
View 2 Replies
View Related
Apr 11, 2015
Ok, so I am writing this program with 10 different functions, and one of those functions needs to count how many times 0 appears in a text file. I've done this before, but I am so stumped right now. Should I get the numbers from the 2d array I have, or should I just use the text file here? Here is what I have right now:
int toursMissed(int scores[][COLS]){
int counter;
for(counter=0;counter<=96;counter++){
if(scores==0){
counter++;
return counter;
}
}
View 7 Replies
View Related
Nov 21, 2013
I am working with C++ in Visual Studio. It's my first semester doing anything like this ever. Still, I am embarrassed that I am having trouble with this simple "coin flipping" program. The user tells the program how many times to "flip the coin" & then the program tells the user the results of each flip. You'll see I am randomly generating a 1 or a 2 within the function coinFlip to represent heads or tails. However, the problem is that if the user wants more than one coin flip, the "random" number stays the same for all of them, resulting in all heads or all tails. I am thinking this is an issue with the for loop that I have within the function coinFlip.
Code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
[Code] .....
View 3 Replies
View Related
Apr 10, 2014
I try to use "rand" to create 100 string, I'm happy I succeed, but next step i wanna know how to count each number of letters and the frequency it shows.
Here is the code:
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <iostream>
// using namespace std;
int main() {
const char *c = "abcdefghijklmnopqrstuvwxyz";
[Code] ......
View 1 Replies
View Related
May 27, 2014
I have a random number generator that should genetate 2 numbers between 100 and 0. The first number works fine but the second doesn't, why?
i1 = random() %100;
i2 = random() %100;
while(i1 < 0 && i1 > 100){
if (i1 < 0)
i1 = i1 + random() %100;
if (i1 > 100)
[Code] .....
View 16 Replies
View Related
Apr 6, 2013
Write a program that generates an array of 20 random numbers between 1 and 100. Use a loop to output the numbers on the screen with a field width of 4 characters. Write code that will reverse the numbers in the array and then output the numbers to the screen with a copy of the same output loop used with the original numbers.
View 7 Replies
View Related
Feb 23, 2015
The program has two array of character. One of them has size 26 and is initialized with the alphabet a to z. A random number generator generates two numbers and these two numbers are used to index into this array of alphabets. The second array is used to hold the indexed alphabets. For example, the first row is the value held in the second array and the second row are the two generated numbers.
b g c z k e <- letter index by generate number n1=1 n2=6 n1=2 n2=25 n1=10 … … … … n2=4 <- generated n1 & n2
Please see illustration below for clarification on the above question.
Element a b c ……… x y z
Index 0 1 2 ……… 23 24 25
Array2
Element a b c ……… x y z
Index 0 1 2 ……… 23 24 25
Final state after program execution
Array1
Element a b c ……… x y z
Index 0 1 2 ……… 23 24 25
Array2*
Element c e m ……… z f g
Index 0 1 2 ……… 23 24 25
Random No generated 2 4 12 ……… 25 5 6
* Actual char element allocated to each index location will depend on corresponding random number generated. Program completes execution after filling all 26 locations in Array2.
View 19 Replies
View Related
Aug 4, 2013
How to produce a code that will satisfy the following constraints. I don't really know C++
- Produces a random three number positive integer between 001 - 504.
- Changes the output number every 24 hours on a website.
View 2 Replies
View Related
Oct 21, 2014
It seems as though my program is working great but I'm having problems with my random number generator. It keeps giving me a return value from the computer of scissors (or 3) for the computers random choice. What can I do?
//Purpose of Program: This program is meant to prompt the user to choose between 4 menu options.
// Each option will be the users choice of rock, paper, or scissors.
// the computer will then randomly generate a number and pit you against the computer.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void user(int getUserChoice);
void comp(int getComputerChoice);
void winner(int UserChoice, int ComputerChoice);
[Code] ....
View 1 Replies
View Related
Mar 5, 2014
How would I go about creating a random number generator (or pseduorandom I suppose) with user inputted lower and upper bounds along with a user inputted size of the sequence (so they can select how many random numbers they'd like outputted)?
View 3 Replies
View Related
Aug 28, 2013
I have to come up with a program that I have to create which is: The Computer guesses a random calculation "Value1 Operator Value2 = Answer." The computer randomly displays two of the four objects. The Player must guess the remaining two objects. If the player gets one right then the computer says "one is right." The game should continue until the user has provided all the correct calculations.
This is what I have, BUT I have 2 errors.
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(static_cast<unsigned int>(time(0))); //Random Number Generator
double value1 = rand() % 50 + 1; //Random number between 1 and 50
[Code] .....
The errors I am getting in Visual Studio are:
error C2181: illegal else without matching if, line 212
IntelliSense: expected a statement, line 212
View 2 Replies
View Related
Jul 13, 2014
I am creating a random number generator and have run into a problem with my do while loop. it continually prints one of the lines of output infinitely. what am i missing here?
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
unsigned seed = time(0);
[code]...
View 7 Replies
View Related
Nov 15, 2013
I've been working on a program on and off for around a week now and I've been struggling towards the end of the program.First of all, the program is a maths quiz which generates two random numbers per question.I'll give you one part of my code:
Code:
srand ( time(NULL) ); //seeds the random number generator
int score = 0;
int a = rand()%12 +1; //generates a random num between 1-12
int b = rand()%12 +1;
int c = a+b;
int d;
}
[code]....
I've basically copied the above code 10 times and changed the variables by going through the alphabet e.g.
Code:
int a = rand()%12 +1; //generates a random num between 1-12
int b = rand()%12 +1;
int c = a+b;
int d; all the way to
Code:
int an = rand()%12 +1;
int ao = rand()%12 +1;
int ap = rand()%12 +1;
int aq = an+ao-ap;
int ar;
Now what I'm going to do is remove all the declared variables and create a loop. But my problem is; If I wanted to declare four variables for e.g.
Code:
int a = rand()%12 +1;
int b = rand()%12 +1;
int c = rand()%12 +1;
int d = a+b-c;
Would I place the srand( time(NULL)); inside the loop? it's confusing because I know an example of a basic loop with an array would be:
Code:
#include <stdio.h>
#include <conio.h>
int main(void)
{
int test[5]={21,18,47,21,4};
int I;
int total=0;
for (I=0;i<5;i++)
total += test[I];
}
[code]....
how or where to include the random number generator in the loop and to make it ask 10 questions at random.
View 1 Replies
View Related
Oct 25, 2013
I am trying to make a random number generator to simulate a critical hit if the number generated is from 0-critical amount. However, every time i run the code, despite being within the parameters set by the if statements, it always runs through as a critical hit and never a regular hit.
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
srand((unsigned)time(0));
[Code]...
There are four attempts to save time in getting a good result.
View 6 Replies
View Related
Aug 29, 2014
I was running some Monte carlo simulations and I was having some trouble passing my Normal-Random-Generating object as a parameter to the simulator.
I created a class called BoxMull which used the Box-Muller algo to generate Normal randoms and I based the Box-Muller algo on random numbers generated by the Mersenne-twister engine.
I am getting an error while compiling.
#include <iostream>
#include <random>
#include <cmath>
const double pi = 3.1428;
template<class rnd_gen>
class BoxMull
[Code] ....
View 6 Replies
View Related
Oct 15, 2014
I need to create a program that has has an array size of 20 and will generate 20 random numbers in it between 1-1000. Then I need to calculate the average of all the random numbers.
I have half of the code done ,but im lost on how to calculate the average of the random numbers.
Here what I have:
#include <iostream>
using namespace std;
int i;
int size[20];
int counter = 0;
int main() {
cout << "The Array Consists Of 20 Random Numbers: "<<endl<<endl;
[Code] ....
View 2 Replies
View Related
Sep 10, 2013
I want to program a program that produces a random number between 1-10, then if the number is correct, says like HEY GOOD JOB and if its not says try AGAIN! also, at any point the user can quit by typing in X.
I know that I can use stuff like cout << "Hey put in a guess now" to prompt the user but I dont know how to accept inputs.
For example, how do I use scanf to do this?
I know that if I use scanf, it takes in a value, but where the heck does it store it?
eg. scanf(%s,guess);
Is that valid? I get an invalid expression error when trying to use that in C++.
View 3 Replies
View Related
Feb 25, 2015
Looping a Nested If w/ random number generator
View 14 Replies
View Related
Aug 9, 2012
I'm trying to create a program that creates random numbers. I looked through some examples and this is what I came up with. The time identifier seems to be undefined yet I see no reason it is undefined.
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
//Re-seed the random-number generator
time_t now;
[code]...
here's my error code..
1>------ Build started: Project: bake, Configuration: Debug Win32 ------
1> bake.cpp
1>c:usersjonbecherdocumentsvisual studio 2012projectsakeakeake.cpp(8): error C3861: 'time': identifier not found
1>c:usersjonbecherdocumentsvisual studio 2012projectsakeakeake.cpp(9): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
View 2 Replies
View Related
Mar 22, 2013
#include <iostream>
#include <stdlib.h>
using namespace std;
[Code]....
View 1 Replies
View Related
Apr 12, 2014
My random number generator isn't working properly. It seems to be ignoring my conditions when it compiles and I enter the input. The program is supposed to accomplish the following.
1. Let the user input how many digits he or she would like to have in the random numbers.
2. Let the user type in how many of the numbers that he or she wants.
3. It will then generate the numbers.
4. It will display the minimum or maximum number it can be with the number of digits the user entered. And display the number of numbers that the user wanted. It also is supposed to check and output only up to the max of that digit range.
so if someone entered they wanted a digit of 1(1-9) but said they wanted 300 numbers it would only output 9
Example:
the user says that she would like 3 digits to be in the numbers generated. So it will output numbers between 100 and 999. then the user says that they would like only 3 random numbers. So it will output three random numbers in between 100 and 999. Also all of the numbers need to be unique so they can't output more then one of the same number.
I am not sure why but it ignores my conditions. Ill type that I want 7 but it just outputs a bunch of random numbers. it dosen't stay in the ranges.
Class file.
Code:
#include "stdafx.h"
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <conio.h>
#include "TargetGen.h"
using namespace std;
[Code] .....
View 6 Replies
View Related
Aug 22, 2014
I wrote the following program shown below that produces a normally distributed random number several times, and then takes the average of them. The problem that I have been having though is that the output it has been producing is 0.0288385 despite the fact that I set the mean of the normal distribution to be 0.1. Why this output value would be so far off from 0.1 despite having averaged over such a large number of random numbers namely 10,000 of them? Also, how to randomly seed this random number generator such that it gives a different value each time its run perhaps by seeding it with the windows timer? Below is the program.
#include <iostream>
#include <random>
using namespace std;
int main() {
default_random_engine generator;
normal_distribution<double> distribution1(0.1,3.0);
double number1,sum,n;
[code].....
View 2 Replies
View Related
Jan 26, 2014
I am writing for loop with a switch so that scores can be inputted in by a judge. The issue that I am running into is that I will put out an the text then the test happens and the code puts out the switch statement 5 times with random number. Here is what I have written.
Code:
int main() {
int diver;
int option;
int Judge;
cout << "Enter Divers Name:";
[Code] ....
View 4 Replies
View Related
May 3, 2013
I've been currently stuck on a C++ problem. Here's the question:
Write a program that generates one hundred random integers between 0 and 9 and displays the count for each number. (Hint: Use rand()
% 10 to generate a random integer between 0 and 9. Use an array of ten integers, say counts, to store the counts for the number of O's, l 's, . .. , 9's.)
I think I'm pretty close, but I keep on getting "0" for the occurrences (or counts) of each random integer.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <fstream>
using namespace std;
const int SIZE = 100;
[Code] .....
View 4 Replies
View Related
Apr 28, 2014
So I wanted to make my own random number generator and decided to use a Blum Blum Shub generator. Read here. Anyways, I implemented it like so.
int blumBlumShub( const int n, int s, int i ) {
if ( i == 0 )
return ( s * s ) % n;
int b = blumBlumShub(n, s, --i);
return ( b * b ) % n);
}
So 'n' is equal to p * q, where in my case, p = 11 and q = 19, so n = 176. 's' was set to 3 and I incremented differently. Using low values of 'i', the numbers were generated quickly, however, after numbers around 25, they took some time to generate. I am aware that this is not supposed to be the fastest PRNG, but it is very very slow (up to minutes) with high numbers.
So, is there a better way to implement this algorithm, or a way to make it faster in general?
Fixed. It generates numbers much faster. The problem was calling the function twice in the return statement.
View 2 Replies
View Related