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

1. use brackets in your if statement to keep two things together, your printout of all 1's and another HoldcountPlaceKeeper

Code:
int howmanytimes = 0;

loop start

if ( something is true)
{
printf(" yep\n");
howmanytimes++;
}

end loop

printf(" how many times it was true is %d\n, howmanytimes);
I'd think by now you should at least understand if statements with and without brackets.

2. Originally Posted by csmith03
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.
post #13

3. you need to slow down and think about what you are doing instaed of just trying to get the quick answers to your problems, as it is not helping you.
Code:
int randNum;
int myRandomNumberArray[100] = { 0 };
int t = 99, g = 0   ;
srand((int)time(0)); // Seeds Random Numbers

// what is this one doing?

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
}

// what is this one doing?

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

}
that is not even correctly executed.
1. declare data types needed
2. fill your data types with the data needed
3. process data by whatever means needed.
this one requires that you find out certain data by its value, and keep track of how many times that took place.then print that out.

you have an array of size 100 that first needs to get filled with a number between 1 - 2. 100 times not 99

then you need to read your data and find the 1's, out of 100 then or while you're doing that you have to keep a count of how may times that occurred then print it out to the screen.

where is the best place / time to keep count of its occurrence?
where is the best time / place to have it print out the total count?

4. Userxbw, thank you. I figured out how to get the print statement to print how many ones there are, but I used too many int variables. I'm only allowed to use TWO int variables and ONE int array.

I feel like I'm so close, but to meet the specifications I'm going to have to completely destroy this code, because I'm not allowed the three int variables, need to replace one with an int array, store the values into the array, then print them in ascending order.

Here's current code:

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

int main(void)
{
int count = 1;
int randNum;
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
if(randNum == 1)
{
printf(" Yes");
count++;
}
else if(randNum == 0)
{
printf(" No");
}

}

printf("\n The number one appears %d times \n", count);

}

5. Originally Posted by csmith03
Userxbw, thank you. I figured out how to get the print statement to print how many ones there are, but I used too many int variables. I'm only allowed to use TWO int variables and ONE int array.

I feel like I'm so close, but to meet the specifications I'm going to have to completely destroy this code, because I'm not allowed the three int variables, need to replace one with an int array, store the values into the array, then print them in ascending order.

Here's current code:

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

int main(void)
{
int count = 1;
int randNum;
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
if(randNum == 1)
{
printf(" Yes");
count++;
}
else if(randNum == 0)
{
printf(" No");
}

}

printf("\n The number one appears %d times \n", count);

}
that is plenty, remember scope, and reuse of the same int variables is valid

Code:
int a = 0;

for ( a = 0; a < 100; a++)
do_something

for ( a = 100; a > 0 ; a--)
do_something
same var used twice, still got one left over. right? Be creative.

that / this last code you're not using your array in a for loop to fill it 100 times then use it in another for or while loop to find the 1's and keep count. ( hint) and you do not need this
Code:
srand((int)time(0)); // Seeds Random Numbers
Code:
rand() % 2;
is enough

6. another point you need to practice your condistions.

Code:
int a;
for ( a = 99; a > 0; a--)
printf('a = %d\n",a);

//or  same thing:

for ( a = 0; a < 99; a++)
printf("a = %d\n", a);

int a = 99;
// Will iterate the conditions until counter hits 0. ( your words)
while(a > 0) {

printf("a = %d\n", a);
a--;
}

you're trying to get 100 out of 99 with a zero based array 0 ... 99 = 100.but what does the evaluations of these really do?
< and > and <= and >= and see exactly where they stop in a loop.

7. Originally Posted by userxbw
and you do not need this
Code:
srand((int)time(0)); // Seeds Random Numbers
Code:
rand() % 2;
is enough
That's true for testing, but it is not true in general: it is indeed necessary to call srand to seed the standard pseudorandom number generator if one wishes to simulate the effect of generating random numbers on each run of the program.

8. Originally Posted by laserlight
That's true for testing, but it is not true in general: it is indeed necessary to call srand to seed the standard pseudorandom number generator if one wishes to simulate the effect of generating random numbers on each run of the program.
not going to argue but I am taking into consideration as well that this has a fix amount of numbers, being only two, 0 or 1, which doesn't leave much room to play. seems a little redundant to me.

9. Originally Posted by userxbw
not going to argue but I am taking into consideration as well that this has a fix amount of numbers, being only two, 0 or 1, which doesn't leave much room to play. seems a little redundant to me.
The PRNG behind srand and rand, whatever the implementation, has a finite range to begin with. You're confusing the range of the PRNG with the notion of simulating randomness. If you don't explicitly seed the PRNG, it will be as if it were seeded with srand(1), hence the result of running the PRNG will be the same on each run of the program. This is great for having consistent input during testing, but is typically bad when you actually want to run the program in "production".

If you don't believe me, compile this program once and run it three times, waiting for some time between each run:
Code:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int i;
for (i = 0; i < 10; ++i)
{
printf("%d ", rand() % 2);
}
putchar('\n');
return 0;
}
Then compile this program once and run it three times, waiting for some time between each run:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
int i;
srand((int)time(0));
for (i = 0; i < 10; ++i)
{
printf("%d ", rand() % 2);
}
putchar('\n');
return 0;
}

10. Originally Posted by laserlight
The PRNG behind srand and rand, whatever the implementation, has a finite range to begin with. You're confusing the range of the PRNG with the notion of simulating randomness. If you don't explicitly seed the PRNG, it will be as if it were seeded with srand(1), hence the result of running the PRNG will be the same on each run of the program. This is great for having consistent input during testing, but is typically bad when you actually want to run the program in "production".

If you don't believe me, compile this program once and run it three times, waiting for some time between each run:
Never said I do not believe you, that is why I didn't argure, I just gave you my logic behind it. You're the college girl, not me ( I'm a boy ) . You're the edecate programmer brain in this, I am just a brain trying to figure it out with scholasticism.

but I am still going to run that program you took your time to write for me to see what I see. thanks!

agree != argue :