# Thread: Could someone help me with randnum()?

1. ## Could someone help me with randnum()?

I'm in an intro to C class and my task is to print 100 random integers between the interval [0,1].

I know how to do the above part, but the second part is what I don't understand.

Using an array, I need to compute the total number of times 1 appeared as a random number.

Here's what I have so far:

I don't know how to use hashtags to post code, I'm new here.

2. Our professor really hasn't gone over arrays with us. The program I have so far generates 100 random numbers between [0,1] -- so each number is either a 1 or a 0.

The part I'm stuck on is printing how many times the integer 1 appears in the 100 numbers.

3. New maybe, but most people seem to figure out this, rather than make excuses.

> The part I'm stuck on is printing how many times the integer 1 appears in the 100 numbers.
You add a counter, and an if statement.

Or since it's only ever 0 or 1, simply adding together all your rands works too.

4. Salem,

Thank you for the response! I will look at the code tags and get better at posting for help.
How do I go about adding up the random numbers calculated by the while loop?

Sorry, I'm not very familiar with C.

Thanks for the help!

5. Well you managed t = t - 1;

Surely you can manage to add another variable and throw in +1

6. The trouble is, I'm only allowed two int variables and one int array. I'm almost positive I need an array, but I'm really unfamiliar how to use it with rand numbers.

I'm supposed to store them in an array and calculate how many times 1 appears.

7. Well an it array looks like
Code:
`int myRandomNumberArray[100] = { 0 };`
If you started t at 99, and used >= 0 as the test, you could use it as a subscript.

8. I've declared the array and changed the while conditions and started t at 99. How do I go about associating an array with the loop to calculate the number of ones?

Here's what i have so far: (Not sure if I did code tags right)

Code:
```#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
int randNum;
int myRandomNumberArray[100] = { 0 };
int t = 99	;
srand((int)time(0)); // Seeds Random Numbers

while(t >= 0) // Will iterate the conditions until counter hits 0.
{
randNum = rand()%2; // Finds random numbers on interval [0,1]
printf("\n Random Number is %d", randNum); // Will display Randon Number found.
t = t-1; // Counter for while condition
}

}```

9. Originally Posted by csmith03
How do I go about associating an array with the loop to calculate the number of ones?
Write a second loop, a for loop preferably, that loops over the array element by element. On each iteration of the loop, it checks if the current element has the value of 1, and if so, it increments a counter that was set to 0 before the loop.

Note that if not for the requirement to use an array, you could do this counting within the while loop that generates the pseudorandom numbers. Of course, you can still do this within that while loop, but it defeats the purpose of the array, so it is likely not the expected approach.

10. if statement to check its value
Code:
```#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
int randNum;
int myRandomNumberArray[100] = { 0 };
int t = 99  ;

//srand((int)time(0)); // Seeds Random Numbers

/* rand() plays on its own */

while(t >= 0) // Will iterate the conditions until counter hits 0.
{
randNum = rand()%2; // Finds random numbers on interval [0,1]
if (randNum == 1)
printf("\n #element %d Random Number is %d",t, randNum); // Will display Randon Number found.
t--; // = t-1; // Counter for while condition
}

}```
simple as that. cheers! where does that Array come into play?

11. I'm limited on my knowledge of for loops, but I created this. I'm not exactly sure how to connect the array and the randNum variable. Also, how do I print the number of times one appears out of the 100 random integers?

Thanks for the help.

Code:
```for(t=0; t<=99; t++)
{
myRandomNumberArray[100] = rand()%2;

}
printf("\nNumber one occurs %d times\n", myRandomNumberArray[t]);```

12. I tried that code, it just gave me an infinite loop where the random number was one every time. My task was to generate 100 random numbers on the interval [0,1] (so basically just 0 or 1) and print how many times the number one appears.

Thanks for the help!

13. Originally Posted by csmith03
I tried that code, it just gave me an infinite loop where the random number was one every time. My task was to generate 100 random numbers on the interval [0,1] (so basically just 0 or 1) and print how many times the number one appears.

Thanks for the help!
you got your length 0 ... 99;
Code:
`array[ number to indicate which element you want the value set inside of it ] = value;`
use your counter to set the element = value
Code:
```// seeings how you set t = 99, to counter act that you could do this
int t = 99 ,g = 0;

for ( t = 99; t > 0; t--)
{
myRandomNumberArray[g++] = (rand()%2);
}```
or just start high ( 99 ) like you got it set and work your way down to zero 0
Code:
```int t = 99;

for ( t = 99; t > 0; t--)
{
myRandomNumberArray[t] = (rand()%2);
}```
but does that actually get you to 100 using 99?

run it two ways to see how this effects your count:
Code:
```for ( t = 99; t > 0; t--)
array[ t ] = t;

// vs
for ( t = 99; t >= 0; t--)
array[ t ] = t;```
then loop out its value to check your element count.
Code:
```for ( t = 0; t < sizeof(array)/sizeof(array[0]); t++)
{

printf("t = %d number element is %d\n",t, array[t]);

}```

14. Thank you, I understand the for statement a little better now.

Right now, it's printing the 100 random integers, then the second print statement is printing "Number one occurs one time", but due to the count it's iterating the print statement 100 times. Is there a way to take the number of times one occurs and print that number in ONE print statement? If that makes sense.

Thanks for the help by the way, I'm new to C and am really just trying to familiarize myself.

Here's my entire code so far:
Code:
```int randNum;
int myRandomNumberArray[100] = { 0 };
int t = 99, g = 0	;
srand((int)time(0)); // Seeds Random Numbers

while(t >= 0) // Will iterate the conditions until counter hits 0.
{
randNum = rand()%2;		// Finds random numbers on interval [0,1]
printf("\n Random Number is %d", randNum); // Will display Randon Number found.
t = t-1; // Counter for while condition
}

for(t=99; t>0; t--)
{
myRandomNumberArray[g++] = (rand()%2);
printf("\nNumber one occurs %d times\n", myRandomNumberArray[g++]);

}```

15. I'm not sure what that means I tried that code and it just printed "t = 1, element is 1". I need the print statement to print "The number of times the integer one occurs from your random numbers is %d"... I just don't know how to associate the array with the 100 random integers on the interval [0,1] and find how many times 1 occurs.