Adding together two arrays and print into third array - c++

Before I start I must notice that I am a begginer in C++.
I have a code (see below), In this code I have two arrays with 10 random numbers but In tab_A numbers are the same like in tab_B - I don't know how to solve this. Also I don't know how to merge/add/sum these two arrays in new array tab_C and print result.
#include <iostream>
#include <cstdio>
#include <time.h>
#include <cstdlib>
using namespace std;
int gen() {
return rand() % 11;
}
int main()
{
int tab_A[10];
cout<<"TABLICA A DEBUG"<<endl;
srand (time(NULL));
for (int i=0; i<10; i++)
{
tab_A[i] = gen();
cout<<tab_A[i]<<endl;
}
int tab_B[10];
cout<<"TABLICA B DEBUG"<<endl;
srand (time(NULL));
for (int i=0; i<10; i++)
{
tab_B[i] = gen();
cout<<tab_B[i]<<endl;
}
int tab_C[10];
cout<<"TABLICA C DEBUG"<<endl;
int sumAB=0;
sumAB=tab_A[10]+tab_B[10];
tab_C[10]=sumAB;
cout<<tab_C[10]<<endl;
return 0;
}

In the code, you have called srand twice with the same seed. Hence, the numbers that will be randomly generated will be the same. If you want to generate random numbers it is advisable to set seed only once.
Also, there seems to be an issue in the code. C++ has 0-indexing. Hence, the lines
sumAB=tab_A[10]+tab_B[10];
tab_C[10]=sumAB;
cout<<tab_C[10]<<endl;
will give errors.
As the size of tab_C is 10 so the index of the last element would be 9.

Related

Trying to stick randomly generated numbers into an array

I'm working on a project currently that involves taking randomly generated numbers, putting them into an array, then using that array throughout the entire program. It's basically supposed to be a number version of wordle. What I'm stuck on right now it trying to make the function to array connection work, and I don't know what I'm doing wrong.
I'm in a beginner course for programming, so I'm probably making a lot of dumb mistakes. The purpose of the function is to generate 5 random numbers between 0 and 9. The I'm supposed to take those numbers and stick then into an array. Then the array needs to be saved so it can be used for a game.
#include <iostream>
#include <random>
#include <chrono>
#include <array>
using namespace std;
//using std::array;
int getRandomDigit() {
std::random_device randomSource;
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine engine(seed);
std::uniform_int_distribution<int> uniformDist(0,9);
//for (int i = 0; i < 5; i++) {
int digits[5] = {getRandomDigit(),getRandomDigit(),getRandomDigit(),getRandomDigit(),getRandomDigit()};
return digits[5];
}
//for (int i = 0; i < 5; i++) {
//int randArray[5] = {getRandomDigit(),getRandomDigit(),getRandomDigit(),getRandomDigit(),getRandomDigit()};
int main()
{
//for (int i = 0; i < 5; i++) {
//int randArray[5] = {getRandomDigit(),getRandomDigit(),getRandomDigit(),getRandomDigit(),getRandomDigit()};
//}
cout << digits[1] <<endl;
return 0;
}
This is what I have so far. I compile it, and it gives me an error of "'digits' was not declared in this scope". I'm honestly very lost on how to make this work. The random number generator was given to us by our instructor
Your compilation error "'digits' was not declared in this scope" is because you are using digits in your main function without declaring it first. You only have it declared in getRandomDigit(). Declare your array in main() instead and set it to values that are returned from getRandomDigit(). Additionally, you can not return an array in C++ directly. Focus on returning a single int from the getRandomDigit() function and populating your array in main().

creating multiple objects with array of structures having unique random numbers

I am trying to create different objects, each with an array of structures of numbers with random values. After compiling I am getting the same sequence of number inside the arrays of every object.
Is there a way to create different objects with unique sequences of numbers inside the arrays?
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
struct storeTwoValue
{
int x;
int y;
};
class practice{
public:
storeTwoValue storageArray[10];
void valueGenerator()
{ srand(time(NULL));
for (int i = 0; i< 10; i++)
{
storageArray[i].x = rand()%10 +1;
storageArray[i].y = rand()%7 + 1;
}
}
void print()
{
cout<<"x"<<" "<<"y"<<endl;
for (int i = 0; i< 10; i++)
{
cout<<storageArray[i].x <<" ";
cout<< storageArray[i].y << endl;
}
cout<<endl;
}
};
int main()
{
for(int i=0; i<3; i++)
{ practice A;
A.valueGenerator();
A.print();
}
return 0;
}
Move the srand() call into main, i.e. execute it only once.
The way you are using it, it gets called in too short order for each object, at least if you create/initialise them all at start. I.e. they get all initialised while time(0) gives identical seeds, which means that the pseudo random number generator is basically reset (starting same sequence from same initial value).
To verify this, you can (before moving the srand call) extend your loop. If it takes enough time, so that time(0) reliably has different values, you will see groups of objects with same values within the groups, but different across groups.
Calling srand() should be done exactly once, calling it more often does not improve randomness.

c++ random number generation not random

I'm trying to perform a random shuffle of a vector using Visual Studio 2013 C++. The following is the code that I have
static void shuffle(vector<int>& a){
int N = a.size();
unsigned long long seed = chrono::system_clock::now().time_since_epoch().count();
default_random_engine generator(seed);
for (int i = 0; i < N; i++){
uniform_int_distribution<int> distribution(0,(N-1)-i);
int r = i + distribution(generator);
swap(a[i], a[r]);
}
}
My problem is when I call this method multiple times in succession the shuffle is not random. What could be wrong with the code?
Any help would be much appreciated.
Uhm, I'm curious... why isn't the following sufficient for your needs:
static void shuffle(vector<int>& a)
{
// There are better options for a seed here, but this is what you used
// in your example and it's not horrible, so we'll stick with it.
auto seed (std::chrono::system_clock::now().time_since_epoch().count());
// Don't bother writing code to swap the elements. Just ask the standard
// library to shuffle the vector for us.
std::shuffle(std::begin(a), std::end(a), std::default_random_engine(seed));
}
std::shuffle dosent remove duplicates, it just swaps the positions of the random numbers generated.
How can I efficiently select several unique random numbers from 1 to 50, excluding x?
You can home cook your own shuffle code otherwise:
#include <ctime>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
void myShuffleWithNoRepeats( int random_once_buf[] , int size=100)
{
srand(time(0));
for (int i=0;i<size;i++)
{
// call made to rand( ) , stored in random_once_buf[ ]
random_once_buf[i]=rand() % 100;
//////////////////////////////////////////////////////////////////////////////////////
// The line below generates unique random number only once //
// //
// the variable i is the random_once_buffer[i] buffer array index count, //
// j is the check for duplicates, j goes through the random_once_buffer[i] buffer //
// from 0 to i at every iteration scanning for duplicates, reversing one step if one duplicate is found.. //
//////////////////////////////////////////////////////////////////////////////////////
for(int j=0;j<i;j++) if (random_once_buf[j] == random_once_buf[i]) i--;
}
cout<<" \n\n\n ";
}
int main(void)
{
const int size=100 ;
int random_once_buffer[100] ;
// Call made to function myShuffleWithNoRepeats( )
myShuffleWithNoRepeats( random_once_buffer , size );
// Loop to display the array random_once_buffer[ ]
for ( int i=0;i<size;i++) cout<<""<<random_once_buffer[i]<<"\t";
cout<<" \nPress any key to continue\n";
cin.ignore();
cin.get();
return 0;
}

Can I make my do while loop create a new random number every time it loops?

Can I make my do while loop create a new number from my pseudo random number every time the loop comes around again? If so, how?'
EDIT: Sorry, it's in C++
EDIT2: I just want a new number between 0 and 3 (0,1,2,3) every time the do...while loop goes around for an integer
While you might like the following example:
do
{
new_number = out_of(my_pseudo_random_number);
}
while(true);
You may find it more useful:
int main()
{
srand(time(NULL)); // Initialize once at program startup.
do
{
int number = rand(); // generate new random number,
}
while(true);
}
(But I'm absolutely not sure what your're asking for)
This will specifically make random numbers from 0 t0 3.
You don't necessarily need the iostream/cout statements except for the output I do.
rand() % 4; creates a random number from 0 to (not including) 4.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
int num;
//initialize random seed
srand(time(NULL));
//make some numbers
do{
num = rand() % 4;
cout << num;
} while(true);
return 0;
}

What's wrong with my random number generator?

I'm just diving into some C++ and I decided to make a random number generator (how random the number is, it really doesn't matter). Most of the code is copied off then net but my newbie eyes cannot see anything wrong with this, is there any way this can be tweaked to give a number other than "6" each time?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int random_number(int min, int max)
{
srand((unsigned)time(0));
int random_num;
int range=(max-min)+1;
random_num = min+int(range*rand()/(RAND_MAX + 1.0));
return random_num;
}
int main()
{
for(int i =0;i < 100;i++)
{
cout << random_number(3,10) << endl;
}
}
Add srand before the loop
srand((unsigned)time(0));
for(int i =0;i < 100;i++)
{
std::cout << random_number(3,10) << endl;
}
Don't call srand() within random_number(). This will re-seed the random number generator every call. For 100 calls, you'll very likely get the same seed every call, and therefore the same number.
The problem is that you use srand everytime. CPU is so fast that it will execute all this code in a single second, so you get the same seed each time.
Move srand out of the loop, call it only once.