# SWIG function problem

**URL:** <https://www.ruby-forum.com/t/swig-function-problem/123318>\
**Category:** Ruby\
**Created:** [December 16, 2007, 10:53am UTC](https://www.ruby-forum.com/t/swig-function-problem/123318 "2007-12-16T10:53:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@fla](https://www.ruby-forum.com/u/fla)\
**Post date:** [December 16, 2007, 10:53am UTC](https://www.ruby-forum.com/t/swig-function-problem/123318/1 "2007-12-16T10:53:57Z")

</div>

Hello,  
I have following Ruby code:  
class Random  
def initialize(init, im=139968, ia=3877, ic=29573)  
@last = init  
@im = im  
@ia = ia  
@ic = ic  
end  
def next(max)  
(max \* (@last = (@last \* @ia + @ic) % @im)) / @im  
end  
end

v = Random.new(42.0)  
10.times do  
puts v.next(1.0)  
end

And I try to write the next-function in c  
float gen\_random(int \*last, float max, int IM, int IA, int IC) {  
\*last = (\*last \* IA + IC) % IM;  
return (max \* \*last / (float)IM);  
}  
and with help SWIG I try make useable in Ruby but I can not.

require ‘fastac’  
…  
def next(max)

# (max \* (@last = (@last \* @ia + @ic) % @im)) / @im

```
 Fastac.gen_random(@last, max, @im, @ia, @ic)

```

end  
…

$ ruby gen\_random.rb  
gen\_random.rb:12:in `gen_random': Expected argument 0 of type int *, but got Fixnum 42 (TypeError) in SWIG method 'gen_random' from gen_random.rb:12:in`next’  
from gen\_random.rb:18  
from gen\_random.rb:17:in `times’  
from gen\_random.rb:17  
[/code]

What I make wrong?

best regards

---

<div class="post-metadata">

**Author:** ![fla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@fla](https://www.ruby-forum.com/u/fla)\
**Post date:** [December 17, 2007, 5:42pm UTC](https://www.ruby-forum.com/t/swig-function-problem/123318/2 "2007-12-17T17:42:41Z")

</div>

On Dec 16, 2007 10:53 AM, Fla As [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And I try to write the next-function in c  
> float gen\_random(int \*last, float max, int IM, int IA, int IC) {  
> \*last = (\*last \* IA + IC) % IM;  
> return (max \* \*last / (float)IM);  
> }  
> and with help SWIG I try make useable in Ruby but I can not.

See [SWIG and Ruby](http://www.swig.org/Doc1.3/Ruby.html#Ruby_nn32)  
(you can reuse your original header if you #ifdef swig directives, see  
swig doc for more info).

If that doesn’t help, please post somewhere your code, including ruby,  
c, and swig header. It would make reproducing your problem easier.

J.

---

<div class="post-metadata">

**Author:** ![fla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@fla](https://www.ruby-forum.com/u/fla)\
**Post date:** [December 20, 2007, 3:36pm UTC](https://www.ruby-forum.com/t/swig-function-problem/123318/3 "2007-12-20T15:36:51Z")

</div>

Your link doesn’t help. My little code is atteched.

Can you please remove my email address from your last message?

Jano S. wrote:

> > And I try to write the next-function in c  
> > float gen\_random(int \*last, float max, int IM, int IA, int IC) {  
> > \*last = (\*last \* IA + IC) % IM;  
> > return (max \* \*last / (float)IM);  
> > }  
> > and with help SWIG I try make useable in Ruby but I can not.
> 
> See [SWIG and Ruby](http://www.swig.org/Doc1.3/Ruby.html#Ruby_nn32)  
> (you can reuse your original header if you #ifdef swig directives, see  
> swig doc for more info).
> 
> If that doesn’t help, please post somewhere your code, including ruby,  
> c, and swig header. It would make reproducing your problem easier.
> 
> J.
