LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2006, 11:27 AM   #1
icylicious
LQ Newbie
 
Registered: Jul 2006
Posts: 12

Rep: Reputation: 0
Question bad: help me urgent just simple assert.h prob


bad: help me urgent just simple assert.h prob

--------------------------------------------------------------------------------

#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...
 
Old 07-24-2006, 11:32 AM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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.
 
Old 07-24-2006, 11:55 AM   #3
icylicious
LQ Newbie
 
Registered: Jul 2006
Posts: 12

Original Poster
Rep: Reputation: 0
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
 
Old 07-24-2006, 12:09 PM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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&section=3
 
Old 07-24-2006, 12:26 PM   #5
icylicious
LQ Newbie
 
Registered: Jul 2006
Posts: 12

Original Poster
Rep: Reputation: 0
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
 
Old 07-24-2006, 12:36 PM   #6
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
It may be giving you the same result because the random number generator has not been 'seeded' at the beginning of each run.
 
Old 07-24-2006, 12:53 PM   #7
icylicious
LQ Newbie
 
Registered: Jul 2006
Posts: 12

Original Poster
Rep: Reputation: 0
hi can u tell me please what "seeded" means.. and is it correct that two computer gives me differet result? which is correct?
thanks
 
Old 07-24-2006, 12:55 PM   #8
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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.
 
Old 07-24-2006, 01:06 PM   #9
icylicious
LQ Newbie
 
Registered: Jul 2006
Posts: 12

Original Poster
Rep: Reputation: 0
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
 
Old 07-24-2006, 01:22 PM   #11
icylicious
LQ Newbie
 
Registered: Jul 2006
Posts: 12

Original Poster
Rep: Reputation: 0
no. ok thanks for the links
 
Old 07-24-2006, 02:53 PM   #12
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Moved to Programming
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bad: help me urgent just simple assert.h prob icylicious Linux - Newbie 3 07-24-2006 01:25 PM
bad: help me urgent just simple assert.h prob icylicious Programming 1 07-24-2006 01:18 PM
URGENT: need simple smb live distro Fraudulent Linux - Enterprise 1 04-13-2005 07:09 AM
prob setting up ipchains for network(Urgent plse) Brian Workman Linux - Security 3 05-01-2002 03:12 AM
A Simple Prob With Modem Mewizard Linux - Newbie 2 05-18-2001 01:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration