LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-08-2009, 07:29 AM   #1
smp
LQ Newbie
 
Registered: Jun 2009
Posts: 21

Rep: Reputation: 0
ran1( ) routine from numerical recipe


Hi,

I have a small C code which uses ran1() routine from numerical recipe.
If i run it, I get the output which is not expected, since ran1() generates random numbers in the interval (0,1).
But I am getting very large numbers instead.

my code is:

Code:
/*           random_no_generator.c             */

#include<stdio.h>
#include<math.h>
#include<stdlib.h>

int main()
{
  long *idum;
  int size;
  long i,j;
  float no;

  j=-5;
  size = 10;
  idum = &j; 

  for(i=1;i<=size;i++)
   {
     no=ran1(&idum);
     printf("%f\n",no);
   }

 return 0;
}
The ran1.c is in the folder in which this code is present.
The compilation command I give is:

Code:
gcc ran1.c  random_no_generator.c -o random_no_generator -lm
Any help will be appreciated.

Thanks and Regards
smp
 
Old 07-08-2009, 09:42 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
If you are using this code:
http://ciks.cbt.nist.gov/bentz/flyash/node14.html

then the rand1 function returns a double, not a float (it's twice as big).
 
Old 07-08-2009, 11:11 AM   #3
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 515

Rep: Reputation: 176Reputation: 176
use "gcc -Wall"

Here's a hint: function declaration
 
Old 07-09-2009, 01:00 AM   #4
smp
LQ Newbie
 
Registered: Jun 2009
Posts: 21

Original Poster
Rep: Reputation: 0
(1)

Quote:
use "gcc -Wall"

Here's a hint: function declaration
I used "gcc -Wall". Then I get following message:

Code:
random_no_generator.c: In function `main':
random_no_generator.c:26: warning: implicit declaration of function `ran1'
When I ran the code (ignoring this warning), I got following numbers, which are of course not the expected numbers in (0,1).

Quote:
1054145984.000000
1035753472.000000
1061266432.000000
1057462912.000000
1064186112.000000
1053055616.000000
1059546944.000000
1032381568.000000
1060700224.000000
1059836032.000000
(2)

When I declared the function ran1() above main() function as:

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

float ran1(long *idum);

int main()
{
Then after compiling the code, I got this warning message:

Quote:
random_no_generator.c: In function `main':
random_no_generator.c:22: warning: passing arg 1 of `ran1' from incompatible pointer type

Again I neglected the warning and ran the code. This time I got the expected numbers:

Code:
0.415999
0.091965
0.756410
0.529700
0.930436
0.383502
0.653919
0.066842
0.722660
0.671149
So what is the wrong thing due to which I got above warning message of incompatible pointer type?


regards
smp
 
Old 07-09-2009, 01:18 AM   #5
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
You need to declare the prototype of the function before you use it.

Moreover, you should not call ran1 like this:
Code:
no=ran1(&idum);
(this is a long **)
but like this:
Code:
no=ran1(idum);
(long *)

knowing the prototype, the compiler have implicitely converted that to a long *.
If you don't tell it the prototype, it does not convert it.

Last edited by Agrouf; 07-09-2009 at 01:24 AM.
 
Old 07-09-2009, 02:25 AM   #6
smp
LQ Newbie
 
Registered: Jun 2009
Posts: 21

Original Poster
Rep: Reputation: 0
Thanks it worked. There is no any warning message.

But the numbers generated are different from the previous set of numbers when I had got the warning message of incompatible pointer type.

Following is the set of numbers for warning-free compilation:

Code:
0.172861
0.680409
0.917078
0.917510
0.766779
0.648501
0.334211
0.505953
0.652182
0.158174

regards
smp
 
Old 07-09-2009, 03:33 AM   #7
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
Aren't those number random numbers? If that is the case, then I expect that they will be different each time you launch the program...
 
Old 07-09-2009, 04:28 AM   #8
smp
LQ Newbie
 
Registered: Jun 2009
Posts: 21

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Agrouf View Post
Aren't those number random numbers? If that is the case, then I expect that they will be different each time you launch the program...
No. Each time, the program is run , I will get the same sequence of random numbers. It is a pseudo random no. generator.

The "seed" effect is not put in this code i.e. for every run of the code, I would have got a different sequence of random numbers.

Regards
smp
 
Old 07-09-2009, 06:31 AM   #9
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
Well, ok but the seed is different:
&idum is not the same as idum
&idum is the address of idum, whereas idum is the value of idum.
In the first case, &idum was translated to a long number that was the address of idum, which is the address of j, whereas in the second case idum is actually j, that is to say -5.
 
Old 07-17-2009, 04:28 PM   #10
caduqued
Member
 
Registered: Apr 2008
Location: Coventry, United Kingdom
Distribution: Slackware64, Slackware64 13.37, linuxslackware
Posts: 82

Rep: Reputation: 19
nice explanation.... thanks a lot to everybody here! I am doing my first steps into C... and these discussions are really helpful...
 
Old 07-19-2009, 08:49 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
This http://www.amazon.com/Book-C-Program...8050932&sr=1-1 is a really good book on C, eg example programs are explained line-by-line.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Three actions in a recipe of Procmail Borelian Linux - Software 1 11-11-2012 07:48 AM
Procmail recipe question gauge73 Linux - Software 3 05-03-2006 05:09 AM
Procmail recipe jebix666 Linux - General 1 06-09-2005 02:43 PM
Numerical recipes in C++ marios_auth Programming 0 07-08-2004 03:19 AM
procmail recipe not working? theLIGHT Linux - Software 3 11-18-2002 11:04 PM


All times are GMT -5. The time now is 01:01 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration