Need help soccer simulation - c++

I need help with a winning condition and randomly placing teams against each other...i randomly selecting teams to play and i keep getting the same teams playing twice or playing itself and idk what to do
#include <iostream>
#include <ctime>
#include <fstream>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <sstream>
using namespace std;
struct teams{//declaring a struct for the teams
string side;
int number;
int number1;
}teams1[16], points[16];
//void intro screen(){//function for the introduction screen
void fileData(){//function for reading the teams data file
ifstream input;
input.open("FootballTeam.txt",ios::in); //associate file
if(input.is_open()){//opening the file
for(int x=0; x<16; x++){//looping through the file
input>>teams1[x].side;//getting info from the file
cout<<teams1[x].side<<endl;//printing out the data from the file
}//end for
}//end if
}//end void
void play(){//function for playing the game
srand(time(NULL));
for(int x=0; x<=1; x++){//loop for random teams to play
for(int s=0; s<=7; s++){//loop for randoms goals value
x=rand() %16+1;//randomly selecting two teams
points[s].number=rand()%4+1;//randomly selecting goals
points[s].number1=rand()%7+3;//randomly selecting goals
cout<<teams1[x].side<<" :"<<points[s].number<<" vs "
<<teams1[s].side<<" :"<<points[s].number1<<endl<<endl;//printing out the teams and goals
//cout<<teams1<<" Won this match"<<endl;
}//end for
}//end for
}//end void
int main (){
cout<<"ROUND OF 16 Finalists!!!\n"<<endl;
fileData();
cout<<"\n";
system("PAUSE");
system("CLS");
play();
return 0;
}//end main

The rand() pseudo-random number generator uses its default seed if you don't call srand() before using rand(). To prevent rand() from using the default seed on every run of your program, and thereby always choosing the same pairs of teams, you should call srand() and pass in time(NULL), which I see you have done. Since your program will never run at the same time twice, rand() will output different numbers on each run.
However, note that you should only call srand() once. So you need to call it in main(), as soon as your program starts up. Right now you're calling srand() every time play() is invoked. The time intervals between each invocation of play() are probably very small. Thus, rand() ends up being seeded with virtually the same number every time, since the time differences are too small. This effectively starts rand() at the same point in its pseudo-random number sequence, which is why you see the same teams playing each other.
int main() {
srand(time(NULL));
// now you're free to use rand() for the rest of the program
// ...
}
See this reference for more information on srand().

Related

Why use "time" in srand?

I like to learn by screwing around with code, recently I copied and pasted a random number generator code. Then I removed all the lines of code that were not "necessary" to make the executable work to generate a random number. The final straw was me deleting "time" from srand.
srand((unsigned) time(0));
What is the point of "time(0)" here?
Does it use the time that the program is opened to generate the seed for the random number? Is that why removing it (time) makes it not work? Because then it doesn't have a seed?
Also...
include <stdlib.h>
include <stdio.h>
include <time.h>
int main()
{
srand((unsigned) time(0));
printf("Your dice has been rolled! You got:");
int result = 1 + (rand() % 20);
printf("%d", result);
}
that's the whole code and I noticed it used the "rand" result for output. Does the "rand" pull the seed from "srand"?
If you don’t “seed” the random number generator (or if you use the same seed value), you’ll get the same set of pseudorandom numbers.
Using the current time is an easy way to get a different seed every time.
The effect of srand cannot cross threads, so the random number seed should be set once on each thread. #Buddy said that using time(0) is the most convenient way to do this, and each call will get a different seed.Of course you can use an atomic variable .
std::atomic<int> seek(2374213); //init whatever you like
void thread1fun()
{
srand(++seek);
//...
int rand_num = rand();
}
void thread2fun()
{
srand(++seek);
//...
int rand_num = rand();
}

How to make a variable have a diferent random between x and y value everytime it is used in the same loop?

I got this homework today. A worker works for an amount of days (inputted by the user). 5% of the days he gets a bonus money (the amount is also inputted by the user). The length of workday is measured in hours. The number of workhours must be different and random for every day.For example day1 the workhours are 8 day2-4 day3-12 etc.
All the user input things are done, but when it comes to the calculations part I've made this loop.
for (int i=0;i<= totalDays;i++)
{
daysWithBonus=totalDays*(5/100)
bonusIncome=daysWithBonus*Bonus
moneyForTimeWorked=timeWorked*paymentForHour
totalIncome=bonusIncome+moneyForTimeWorked
}
Now my problem is that I can not get the timeWorked variable to be random and different for every day.I found this in the web:
int timeWorked = rand() % 9 + 3;
but it does not seem to work. Every time I run the program I get 8 for the value of the variable for each day. Can someone help me figure this out?
Because rand is pseudorandom. It is the same for every run so you can do better debugging for example.
Take a look at srand, and use the time of your pc as a seed.
That is: add srand(time(0)); to your code.
Edit: Reference this question.
It's important to understand that rand() is "pseudorandom." Give it a proper seed value (like the current time) via srand() and then it should give you random values, like so:
/* srand example */
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
int main ()
{
printf ("First number: %d\n", rand()%100);
srand (time(NULL));
printf ("Random number: %d\n", rand()%100);
srand (1);
printf ("Again the first number: %d\n", rand()%100);
return 0;
}
From here: http://www.cplusplus.com/reference/cstdlib/srand/
I think using rand() in the context of simple schoolwork is fine. And as was previously pointed out, you need to be careful when calling rand() repeatedly (like in a loop) as this may give you duplicate numbers.

Autoclicker using C++ to click in differnent intervals using RNG * Milliseconds to determine the time between clicks

I've been testing it for hours now and I just can't get it to work.
I'm trying to get an Autoclicker to work, using C++.
Said Program should be able to click without moving the mouse, not specified in the Programm.
The mouse only needs to left-click.
This has to be done in different intervals. My idea was, that we use a Random Number Generator (RNG) to determine a Number between 1 and 100.
This step is easy just using a (rand() % 6) + 1 command.
Loading the library you can use the command srand(time(0)) before the for function to actually randomize it.
My problem lies after that.
How to I get the resulting random number to be multiplied by Milliseconds (preferably also randomized) and then use the resulting time as the time beween mouse outputs?
I know this is much to ask but I'm pretty new to coding and this is a passion project. If anyone would be experienced enough to help me out with my problem it would be greatly appreciated.
Edit: This is what I have so far, I know it's not much but it works. Improvements are obviously welcome though.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
/* function main begins program execution */
int main(void) {
int i;
srand(time(0));
for (i = 1; i < 2; i++) {
printf("%d ", 1 + (rand() % 6));
}
cin.get();
return 0;
}

Program works only if I keep displaying something until solution is found

I have made a program that generates a random array of numbers. I am aware that if I make a for structure to place random numbers, there might be a chance that I will have values that will repeat. For that I made a separate recursive function that keeps looking for duplicates and replace them with other values until there will be only distinct numbers:
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int n, m, k, row[100];
int verif(int v[100], int k)
{
for(int i=0; i<k-1; i++)
for(int j=i+1; j<k; j++)
if(v[i]==v[j])
{
cout<<".";
srand(time(NULL));
v[i]=rand()%100+1;
return verif(v, k);
}
return 1;
}
int main()
{
k=10, n=10;
srand(time(NULL));
row[0]=rand()%n+1;
srand(row[0]);
for(int i=1; i<k; i++)
{
srand(row[i-1]);
row[i]=rand()%n+1;
}
verif(row, k);
for(int i=0; i<k; i++)
cout<<row[i]<<" ";
return 0;
}
I hope you can explain me why does a simple cout inside verif makes my program to work and why without it nothing works.
This is a result of calling a function recursively for an extended period of time, likely triggering a stack overflow on your machine.
This expression
srand(time(NULL));
seeds the random number generator for rand() to the value of the present number of seconds since the epoch. Naturally this number changes only once per second. Reseeding the rand generator will get you the same sequence as last time, so the result of rand()%100 + 1 on the following line will remain the same until the result of time(NULL) changes.
Now that you've ensured that v[i]==v[j] will be true until at least a second has passed since the initial assignment, you recursively call this function again, (and again (and again...)) until you get a new value from your "random" number generating procedure (at least a second has passed) or you run out of space on the stack. (you crash)
You can clearly see this happening much more clearly by using a function which holds up execution for longer than is taken by the cout statement, for instance
std::this_thread::sleep_for(std::chrono::milliseconds{500});
You can see such an effect here on coliru
With the sleeping function (which holds the execution up for 500 milliseconds every recursion) you get ~13 recursions.
Without it you get ~2846000 recursions. That's quite a difference.
There are better ways to get a set of non-repeating random numbers in a range, as shown in the answers to this question. I'm partial to Darklighter's answer myself. For that answer if you want, say, 10 elements out of 100, generate the vector with 100 elements, shuffle and then resize it to include only the first 10.
The cout solves the problem because it takes time to print something on the console. All the rest of the code takes roughly 0 time, thus each time you do
srand(time(NULL));
v[i]=rand()%100+1;
You will get exactly the same number, as you always use the same seed. Then I am not 100% sure what happens, it might be a stackoverflow because you keep calling the function recursively forever, or it might be something else. Actually it doesnt really matter, because the real problem is your seed.
Only with the cout you will eventually get a differnt seed and also a different random number.
You should seed the rng only once. And btw if you want random numbers you should use the rngs from <random> and not rand (actually it should be deprecated because of its low quality).

C++ random int function

Hello dear members of stackoverflow I've recently started learning C++, today I wrote a little game but my random function doesn't work properly. When I call my random function more than once it doesn't re-generate a number instead, it prints the same number over and over again. How can I solve this problem without using for loop?
Thanks
#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
int rolld6();
int main()
{
cout<<rolld6()<<endl;
cout<<rolld6()<<endl;
system("PAUSE");
return 0;
}
int rolld6()
{
srand(time(NULL));
return rand() % 6 + 1;;
}
srand(time(NULL)); should usually be done once at the start of main() and never again.
The way you have it will give you the same number every time you call rolld6 in the same second, which could be a lot of times and, in your sample, is near guaranteed since you call it twice in quick succession.
Try this:
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <stdlib.h>
int rolld6 (void) {
return rand() % 6 + 1;
}
int main (void) {
srand (time (NULL));
std::cout << rolld6() << std::endl;
std::cout << rolld6() << std::endl;
system ("PAUSE");
return 0;
}
One other thing to keep in mind is if you run this program itself twice in quick succession. If the time hasn't changed, you'll get the same two numbers in both runs. That's only usually a problem when you have a script running the program multiple times and the program itself is short lived.
For example, if you took out your system() call and had a cmd.exe script which called it thrice, you might see something like:
1
5
1
5
1
5
It's not something you usually do but it should be kept in mind on the off chance that the scenario pops up.
You are constantly reseeding the random number generator. Only call srand(time(NULL)); once at the beginning of your program.
Random functions (no matter the language) are only partially random.
in every technology you will have a equivalent to
srand(time(NULL));
This piece of codes seeds the random function to a start value and then the numbers a generated from there onwards
this means if your always reseeding form the same value you'll always get the same numbers
In your case you want to do something like this (calling srand(time(NULL)); only once).
int rolld6 (void) {
return rand() % 6 + 1;;
}
int main (void) {
srand (time (NULL));
...
//call your function here
}
one of the advantage of seeding with the same value is to offer the possibility to regenerate the same sequence of random numbers.
in one of my games, I would randomly place objects on the screen, but I also wanted to implement a retry option. this options of reseeding from the same value allows me to redo it without storing all the random values ^^