Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Number Generator 1

Status
Not open for further replies.

MGicles

Vendor
Sep 13, 1999
4
US
I need some code help writing a random number generator. I<br>
have never tried one before.<br>
<br>
Using turbo C++<br>
Thanks
 
Do you want to write your own code or use the function random(int num) that comes with borland C++? <br>
random(num) returns a random number between 0 and (num - 1). <br>
randomize() initializes the random number generator with a random value. ;)<br>
<br>
<br>
<br>
<br>

 
zBuilder,<br>
I want to use the random(int num) function, but I am not familiar with how to use it. I kinda know what it does, and I know that I have to use stdlib.h, but I don't really know how to declare it.<br>
<br>
CR
 
You don't have to declare the random(int num) function, it is already declared in stdlib.h<br>
Below is some code to help you pick your lotto #'s<br>
(Note: it's not bullet proof because it doesn't check to see if the same # is picked twice, but it is simple and should give you the right idea about using the random function.)<br>
<br>
#include &lt;stdio.h&gt;<br>
#include &lt;stdlib.h&gt;<br>
<br>
void main()<br>
{<br>
int x, YourNumber;<br>
<br>
randomize(); //initialize the random # generator<br>
printf ("Your 649 numbers are:\n\n");<br>
for ( x = 1; x &lt; 7; x++) // count out 6 random #'s<br>
{<br>
YourNumber = random(48) + 1; //Get a # between 1 & 49<br>
printf( "%d: %d\n", x, YourNumber );<br>
}<br>
}
 
I am only a student, but the program that I am working on now uses rand(). It is a program to help elementary students with their math. For difficulty level 1 it generates two numbers 0-9 to add, subtract, etc... For level 2, it generates two numbers 0-99. Here is the if statement that I am using to do this. rand()will generate<br>
random numbers from 0-32000. I hope that this helps!!<br>
<br>
if (difficulty == 1)<br>
{<br>
x = rand()%10;<br>
y = rand()%10;<br>
}<br>
else<br>
{<br>
x = rand()%100;<br>
y = rand()%100;<br>
}<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top