+6

Randomize functions (C++)

HI, i have a quistion. I keep understanding the rand() or srand(time(0)) function better, but just for numbers. now i am making a prgram that randomly chooses a function to exicute. but how would i do this. i've made som int and void functions but how do i randomly choose one of them? Thanks in advance!

3/4/2017 7:02:23 PM

Steyn

19 Answers

New Answer

+8

use a switch statement, first generate a randomnumber, use the modulo operator sothat you only have as many random numbers as you have functions, then switch on the random number. case 0 - random number

+7

#include <iostream> #include <cstdlib> #include <ctime> typedef int (*singleParam) (int a); int function(int var){ return var; } int fDouble(int var){ return var * 2; } int fSplit(int var){ return var / 2; } int main(int argc, char *argv[]){ unsigned int seedGen; int enterDigit; std::cout << "Enter a number: "; std::cin >> enterDigit; seedGen = time(0); srand(seedGen); enterDigit = rand() % 3 + 1; singleParam functions[]{function, fDouble, fSplit}; for(int i = 0; i < 3; ++i){ std::cout << functions[i](enterDigit)<< std::endl; } return 0; }

+7

Making an array of function pointers or function objects and selecting randomly from it maybe? But you said that there are some int and void functions so I don't know if it is possible.

+3

It would help if you showed me what you have so far written in code. From what I am reading you may need a bit of STL to accomplish it efficiently which is beyond the scope of this website.

+2

using arrays... take a look at my bio generator code

+2

A proof of concept: https://code.sololearn.com/cRpRVc95f44j/?ref=app

+1

As you can't generate a truly random integer with the rand() function alone you have to seed it by including the time library: #include <time.h> including the c standard library: #include <cstdlib> and then the number generator: srand(time(NULL)); int random = rand() % 100 + 1;

+1

//here you go #include<iostream> #include<time.h> #include<cstdlib> using namespace std; void functionOne(){ cout<<"i am function one"; } void functionTwo(){ cout<<"i am function two"; } int main(){ int x; srand( time(NULL) ); x=rand()%2; switch(x){ case 0:{ functionOne(); break; } case 1:{ functionTwo(); break; } } return 0; }

+1

maybe you can use" if " ,or use " switch ". for example: if(rand()==number) call function;

0

what's​ the difference anyway between rand() and srand() function ??

0

can anybody explain me what is this rand() function....and when it can be used?

0

i dont have a answer,because i newbie in this..can teach me

0

Hello can someone upload?form here how to create a dictionary wordlist.txt well, for example a dictionary BiPrUgZr 300 thousand words is no more thanks in advance

0

but what can be the most probable answer in randomize(); int val; val=rand(3)+2; char guess[]="abcdefghijk"; for (int i=1;i<=val;i++) { for(int j=val;j<=7;j++) cout<< guess[j]; cout<<endl;} }

0

if asked in exam how can we predict the output of a rand()function in an objective based question

0

Use random (0,x) and check it in switch. That's all.

0

Please, never use rand()/srand() and then operator%, because you will only get the least random bits possible. To know how to do it the proper way, search about std::uniform_random_distribution() and Mersenne's twister (should be std::mt19337() from the top of my head). The cppreference.com and stackoverflow.com are your best friends here. :)

0

switch and modulo or array and modulo can help you.

0

This is my code (i made it shorter but the idea will be clear) #include <iostream> using namepsace std; void functionOne(){ cout << "I am function one" } void functionTwo (){ cout << "I am function two" } now how do i randomize that everytime my program loads, one of these functions will start but randomly so i never know wich one