ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main (void)
{
int a, b, cnt=0, i;
for(i=0; i <1000; ++i){
a= rand() % 3 + 1;
b= rand() % 30 + 1;
if (b - a <= 1)
continue;
assert (b - a > 2);
printf("count=%d\n", ++cnt);
}
return 0;
}
that's the program and i cant understand how it works.please help me answer these questions too, 1.what values of a and b cause the assertion to fail. 2. how many times do u expect to go through the loop? number of times the program is executed? and the number of iterations produced per execution of the program?
guys please help me thanks...
At which part are you having trouble understanding the code?
We will not give answers to questions that appear to come directly from homework. There is no evidence to support that us doing the work helps you learn the material.
because i have tried running the program several times using two different computers. one gave me result of count=51, the other gave count=2. i want to understand why it gives that result? and how can i track the number of times the program is executed when i dont know how the function rand() works. i just wanna know how the function rand() works.Any site you know that can explain this? i have searched the internet about articles about rand(), assert.h but no explanation about this all i see are codes too. i have homework to make a program that requires random generated numbers, so while researching about it i saw the exercise above under assertion and rand chapter. but also is not helping me. i hope you can help me. thanks
rand() has nothing to do with assert.h. rand() returns a random (well, pseudorandom) number. As a result, every run of the program will get different results. Take a look at the following documentation: http://man.linuxquestions.org/?query...pe=2§ion=3
but i wonder why if i use the same computer it gives the same number/ result all the time. always count=51 when i use linux and alwys count=2 when i use gcc compiler.anyway thanks i'll take a look at the documentation. thanks
Because it is RANDOM, you will always get different results. "Seeding" is setting up a random number generator. Have you read up on random number generators?
Also, please stop posting the same question in multiple forums. It is rude and unneccessary.
oh ok sorry about posting same question in multiple forums. i read some rand() topics but none of it tackle random number generator. that's why i'm asking for your help. i just want to understand how random number generator works so i can answer the problem above. thanks
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.