Sunday, July 12, 2009

Can someone help me c++ stuff?

can someone give me a small example code to put in a random number, and have it generate 1 - 15


number every time it runs that part of the code

Can someone help me c++ stuff?
Mod works good, really good.





You can also google the c++ Rand function, and find out how to set it to give you 0-15, something to do with it's seed, I haven't done c++ in a while
Reply:Modulus 15 will give you a number from 0 to 14, then add 1.





(random % 15) + 1
Reply:There is a standard library in C++ which will generate random numbers which would probably suit your requirements, although it should be noted that the two methods it uses rand() and srand() are far from precise and thus would be hopeless if you were doing a serious statistical analysis. That being said, here is an example using the srand() method.





Note: To use this generator you must include %26lt;cstdlib%26gt; in your program otherwise it will not work.





#include %26lt;iostream%26gt;


#include %26lt;ctime%26gt;


#include %26lt;cstdlib%26gt;





using namespace std;





int main()


{


/** The srand() method will repeat itself every time you start the program thus you would in effect get the same random numbers all the time. Not exactly random is it!? However if you seed this method using an argument between the ( ) braces then you can force the machine into being a little more random because you can seed it with a start point that must be random by the very nature of the argument you place in the ( ) braces. In this example the seed is your Systems internal clock time which logically is different each time you run the programme.





The rand() method does the generating of the random numbers from 0 to RAND_MAX which in most, but not all compilers is 32,767. */





srand((unsigned)time(0));


int random_integer;





/** Int lowest and int highest are set to the values you want. You mentioned 1 to 15, thus the highest module is 15 in this sample. Don't forget to add +1 to the function though becaue computers begin at zero. */





int lowest=1, highest=15;


int range=(highest-lowest)+1;





/** index %26lt; 15 here sets how many random numbers will be produced. Set that to whatever you like. */





for(int index = 0; index %26lt; 15; index++){





/** Make these next two lines ONE continuous line when you type it in your editor */





random_integer = lowest+int


  (range*rand()/(RAND_MAX + 1.0));





cout %26lt;%26lt; random_integer %26lt;%26lt; endl;


}


}





By the way I mentioned most, but not all compilers will produce random numbers from 0 to 32767. If you are wondering what your compiler range is then use this code to find out.





#include %26lt;cstdlib%26gt;


#include %26lt;iostream%26gt;





using namespace std;





int main()


{





/** Type these two lines as ONE continuous line in your editor */





cout %26lt;%26lt; "The value of RAND_MAX is "


%26lt;%26lt; RAND_MAX %26lt;%26lt; endl;


}


No comments:

Post a Comment