random number generator that doesn't repeat it self (cpp/c++) - c++

I want to make a function that generates numbers but doesn't repeat it self. If every number is generated the array can be emptied and it can start over again.
This is the code I made but it doesn't work.
The comments in the code explains the code a little.
The largest number that is allowed is "howManyWords".
This is used to display words which are stored in an array
I want to use it like this: array\[random()\]
#include <stdio.h>
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
//public scope
int howManyWords; // how many words you have enter
int random(){
int random;
int numbers[howManyWords];
srand(time(0)); //changing the algorithm
random = rand() % howManyWords;
numbers[random] = random; // store the digit in the position in the array equal to the digit that is generated
for(int i=0; i<howManyWords; i++){ // going through every element in the array
if(numbers[i] == random){ // if the number is already generated, generate a different number
random = rand() % howManyWords;
}
}
return random;
}

Rather than your function, which discards the state of which numbers it has returned each time it is called, you should use a function object.
struct random_t {
random_t(int max) : values(max), current(max) {
std::iota(values.begin(), values.end(), 0);
}
template<typename URBG = std::random_device &>
int operator()(URBG&& urbg = default_random) {
if (current == values.size()) {
shuffle(std::forward<URBG>(urbg));
}
return values[current++];
}
private:
template<typename URBG>
void shuffle(URBG&& urbg) {
std::shuffle(values.begin(), values.end(), std::forward<URBG>(urbg));
current = 0;
}
std::vector<int> values;
std::vector<int>::size_type current;
static thread_local std::random_device default_random;
};
See it live

Related

No Output from Array Loop

I am to draw 3 balls from an urn of 24 and add up the total discount the balls represent. I am not allowed to replace the balls after drawing, so there can be no duplicate balls drawn. The discounts are 1, 2, 3, 4, 5, 6, 7, 8, and there are 3 of each.
My solution to this problem was to create multiple while loops that roll out of 24 and re-roll until no duplicates are drawn. I have done a loop like this in the past that would loop random numbers until a non-duplicate was chosen and it worked fine, so I set this program up the same way.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int draw(int urn[], int draws[]);
int main()
{
int discountSum,discount;
int urn[24]={1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8};
int draws[1000]={0};
draw(urn,draws);
cin >> discount;
int averageDiscount=discount/1000;
return 0;
}
int draw(int urn[], int draws[])
{
static int discountSum=0;
int i; //simulations counter
int ball1, ball2, ball3;
int score1, score2, score3;
int totalDiscount;
srand(time(0));
for(i=0;i<1000;i++)
{
ball1=rand()%24;
while (ball2==ball1)
{
ball2=rand()%24;
}
while ((ball3==ball2)||(ball3==ball1))
{
ball3=rand()%24;
}
score1=urn[ball1];
score2=urn[ball2];
score3=urn[ball3];
totalDiscount=score1+score2+score3;
draws[i]=totalDiscount;
cout << "1) " << draws[i] << " percent discount.\n";
discountSum+=totalDiscount;
}
return discountSum;
}
When I run this code, instead of receiving an error, the program runs with no output, and a return of a negative garbage value instead of 0. I would like to see the output from each discount up to the 1000th loop.
It looks as if the bug is that ball2 and ball3 are compared before they're ever set to anything. They're never even initialized, which is the likely cause of your "garbage value." It'll probably work if you "draw" each ball before comparing it, as:
ball1=rand()%24;
ball2=rand()%24;
while (ball2==ball1)
{
ball2=rand()%24;
}
ball3=rand()%24;
while ((ball3==ball2)||(ball3==ball1))
{
ball3=rand()%24;
}
An even better way would be to use do...while loops instead, which always run once, as:
ball1=rand()%24;
do
{
ball2=rand()%24;
} while (ball2==ball1);
do
{
ball3=rand()%24;
} while ((ball3==ball2)||(ball3==ball1));
A much simpler way to do this is to use the facilities build into the C++ standard library:
#include <iostream>
#include <algorithm>
#include <random>
std::mt19937 gen(std::random_device{}()); // initialize random number generator
/*
* or
std::mt19937 gen(std::time(NULL));
* if std::random_device is not well-implemented
*/
int draw()
{
static int urn[24]={1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8};
std::shuffle(std::begin(urn), std::end(urn), gen); // shuffle array
return urn [0] + urn[1] + urn[2]; // pick first three balls from array
}
int main()
{
int discount = draw();
// Use discount. For example,
std::cout << discount;
return 0;
}
Documentation:
std::mt19937
std::random_device
std::shuffle

C++ generate random number every time

I need to make a simulator for a college homework. In this simulator there are 3 computers, 2 of which send messages to computer 1 which then decides to either send the message or reject it. The rejection is random chance with 20% of rejection on computer 2 and 50% on computer 3. I use the rand()%100+1 function with the srand(time(NULL)) seed. It makes a random number however I need to run this multiple times and every time the same random number is used. For example if I run the simulation 12 times and the number generated is 45, 45 is used 12 times. I've both placed the random number generator inside the code and made a function outside.
How can you make a random number generator that generates a random number every time?
#include <iostream>
#include <new>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
int randomgen()
{
int rndnum=0;
srand (time(NULL));
rndnum=rand()%100+1;
cout<< rndnum<<endl;
return rndnum;
}
struct comp1
{
int rejected=0;
int received=0;
int sent=0;
int onhold=0;
bool comp2reception()
{
received++;
bool result=false;
int rndnum=0;
srand (time(NULL));
rndnum=rand()%100+1;
if(rndnum<=20)
{
rejected++;
result=false;
}
if(rndnum>=21)
{
onhold++;
result=true;
}
return result;
}
bool comp3reception()
{
received++;
bool result=false;
int rndnum=randomgen;
if(rndnum<=50)
{
rejected++;
result=false;
}
if(rndnum>=51)
{
onhold++;
result=true;
}
return result;
}
};
Use the C++11 header <random>.
#include <random>
static std::random_device rd;
static std::mt19937 gen(rd());
int randomgen()
{
std::uniform_int_distribution<> dis(0,100);
int rndnum = dis(gen);
std::cout << rndnum << std::endl;
return rndnum;
}
Call srand(time(NULL)) just once at the beginning of the program.
Since time() return number of seconds from 1970 and you program probably takes less than that to finish, you esentially reset the generator with same value before each call. Which of course returns same (first) value each call.

combinations of k-tuple from n elements set by recursive

#include <vector>
#include <iostream>
using namespace std;
void SubSetNum(bool * select, int*a, int selectk, int k, int selectn, int n )// depthk to
{
if(k>n) return;
if(selectn==n)
{
if(selectk==k)
{
for(int i=0;i<n;i++)
if(select[i]==true)
cout<<a[i];
cout<<endl;
}
return;
}
select[selectk]=false;
SubSetNum(select,a,selectk,k,selectn+1,n);
select[selectk]=true;
SubSetNum(select,a,selectk+1,k,selectn+1,n);
}
int main()
{
int k=3;
int n=5;
int a[]={1,5,8,10,13};
//while(cin>>k)
{
bool *select=new bool[n];
memset(select,0,sizeof(bool)*n);
SubSetNum(select,a,0,k,0,n);
delete []select;
}
return 0;
}
This a question, that I want to get k elements from n elements set.
But it prints out incorrect answer? I am always confused when I design recursive algorithms...Especially the parameter of functions, if or not return value, and so on, thus I always try to forcely remember the code in textbook.
Your mistake is here:
select[selectk]=false;
...
select[selectk]=true;
It should be this:
select[selectn]=false;
...
select[selectn]=true;
I believe the cause of the mistake was a failure to remember what the variables represent. The variable selectn is the index of the element being included or excluded. The variable selectk is the number of elements already included. It does not make sense to use selectk as an index into a.

How to use std::transform() function

Ok basically i need to use the std::transform() function. I am trying however to pass an object function in as the last argument though and it doesnt seem to work.
class isprime {
public:
// declares the constructor and the operator() overloading
isprime(){ number = 0, primes.push_back(2);};
bool operator()(int);
private:
// various private functions and variables
int number;
list<int> primes; //creats a list via the stl library
void morePrimes(int); //function to generate more prime numbers
bool it; // Iterator for the morePrimes list
bool primeCheck; // Bool used in the morePrimes function
};
bool isprime::operator()(int number)
{
if(number == 1) //returns false for 1
return false;
if(number > primes.back()){ //Tests to see if the list of primes range would include the number, if not it runs the morePrimes function
morePrimes(number);
}
it = binary_search(primes.begin(),primes.end(),number); //uses stl find to see if the number is in the list of primes.
if(it) //if the returned iterator points to a value = to number then number is prim.
return true;
return false; //returns false if the number wasnt found
};
transform(random_list.begin(), random_list.end(), isprime_list.begin(), test());
Above i have included the class, the function and the call i used for transform. Anyone know why this doesnt work? I cant figure it out.
If you provide void isprime::more_primes(int) somewhere, removing the parentheses in test() is likely enough:
int main()
{
auto gen = bind(uniform_int_distribution<>(), mt19937());
list<int> random_list;
for (int i=0; i<5; i++) random_list.push_back(gen());
list<int> isprime_list(random_list.size());
isprime test;
transform(random_list.begin(), random_list.end(), isprime_list.begin(), test);
}
See it 'running' (well, compiling) on Coliru: http://coliru.stacked-crooked.com/a/3d908cb5cdc5c543
#include <list>
#include <algorithm>
using namespace std;
class isprime {
public:
// declares the constructor and the operator() overloading
isprime(){ number = 0, primes.push_back(2);};
bool operator()(int);
private:
// various private functions and variables
int number;
list<int> primes; // creats a list via the stl library
void morePrimes(int); // function to generate more prime numbers
bool it; // Iterator for the morePrimes list
bool primeCheck; // Bool used in the morePrimes function
};
bool isprime::operator()(int number)
{
if(number == 1) // returns false for 1
return false;
if(number > primes.back()){ // Tests to see if the list of primes range would include the number, if not it runs the morePrimes function
morePrimes(number);
}
it = binary_search(primes.begin(),primes.end(),number); // uses stl find to see if the number is in the list of primes.
if(it) // if the returned iterator points to a value = to number then number is prim.
return true;
return false; // returns false if the number wasnt found
}
void isprime::morePrimes(int) {}
#include <random>
#include <functional>
#include <iostream>
#include <iterator>
int main()
{
auto gen = bind(uniform_int_distribution<>(), mt19937());
list<int> random_list;
for (int i=0; i<5; i++) random_list.push_back(gen());
list<int> isprime_list(random_list.size());
isprime test;
transform(random_list.begin(), random_list.end(), isprime_list.begin(), test);
}

maintaining a recursion count

I'm trying to count the number of calls within a recursive permutation function.
I've written a function that fills a queue with all the permutations but I can't seem to figure out how to maintain an accurate count.
Ultimately i'd like the function to return a subset of the permuatations specified by lbound and ubound arguments, and to do so I think i need someway to keep an internal count.
Using the size of the returned queue will not work since i'd like the function to be able to handle permutations too big to hold in memory.
For this code i'd like the count to be returned as 100.
#include <vector>
#include <iostream>;
using namespace std;
int& Permutations(vector<vector<int>> param, vector<vector<int>> &perm, int index=0)
{
static vector<int> iter;
static int count = 0;
if (index == param.size())
{
perm.push_back(iter); // add permutation to queue
count++;
return count;
}
for (int i=param[index][0]; i<=param[index][1]; i+=param[index][2])
{
if (iter.size() > index) iter[index] = i;
else iter.push_back(i);
Permutations(param, perm, index+1); // recursive function
}
}
void main()
{
vector<vector<int>> params; // vector of parameter vectors
vector<int> param1, param2;
int arr1[3] = {0,9,1}; // range for each parameter vector
int arr2[3] = {0,9,1}; // specified as lbound, ubound, step
param1.insert(param1.end(),arr1,arr1+3);
param2.insert(param2.end(),arr2,arr2+3);
params.push_back(param1);
params.push_back(param2);
vector<vector<int>> queue; // queue of generated permutations
int permcount = Permutations(params,queue);
cout << "the permutation count is " << permcount << endl;
cin.get();
}
Using a static count will not work, because it's not going to ever be reset (and will cause problems if you ever go multi-threaded).
Instead, how about this:
int Permutation(/* params */)
{
int count = 1; // Count ourself
for (whatever)
{
count += Permutation(whatever); // Count cumulative sum from recursion
}
return count;
}
Each call to Permutation() returns the total number of calls that were made below it in the call tree. As we unwind, all the counts from the sub-trees get summed together, to eventually produce the final return value.
int foo(int count,/*Other Params*/) {
/*Calucation*/
if (!terminatingCondition) {
foo(count++,/*Other Params*/);
}
logger.log("foo was called " + count + "times");
return /*calcualtion*/;
}
I'm just trying to answer the question by ignoring your actual algorithm purpose. The two statics should be moved to argument references, or you don't have a good way to reset their values.
void Permutations(vector<vector<int>> param, vector<vector<int>> &perm, vector<int> &iter, int &count, int index=0)
{
++count;
// ...
}