# C rand() in Ruby

**URL:** <https://www.ruby-forum.com/t/c-rand-in-ruby/134423>\
**Category:** Ruby\
**Created:** [April 10, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/c-rand-in-ruby/134423 "2008-04-10T18:21:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [April 10, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/c-rand-in-ruby/134423/1 "2008-04-10T18:21:59Z")

</div>

Is there some simple way to make use of C’s rand() from within Ruby?  
The daughter of someone I know is writing some toy scripts to compare  
PRNG results for a science fair project . . .

---

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [April 10, 2008, 8:32pm UTC](https://www.ruby-forum.com/t/c-rand-in-ruby/134423/2 "2008-04-10T20:32:36Z")

</div>

ara.t.howard wrote:

> On Apr 10, 2008, at 10:20 AM, Chad P. wrote:
> 
> > Is there some simple way to make use of C’s rand() from within Ruby?  
> > The daughter of someone I know is writing some toy scripts to compare  
> > PRNG results for a science fair project . . .
> 
> you mean other than just using
> 
> rand

That would call ruby’s mersenne twister, AFAIK.

---

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [April 10, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/c-rand-in-ruby/134423/3 "2008-04-10T20:28:29Z")

</div>

On Apr 10, 2008, at 10:20 AM, Chad P. wrote:

> Is there some simple way to make use of C’s rand() from within Ruby?  
> The daughter of someone I know is writing some toy scripts to compare  
> PRNG results for a science fair project . . .

you mean other than just using

rand

??

you _can_ directly call the C function using ruby/dl (see examples in  
dist)

cheers.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [April 10, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/c-rand-in-ruby/134423/4 "2008-04-10T21:02:02Z")

</div>

On Fri, Apr 11, 2008 at 03:27:53AM +0900, ara.t.howard wrote:

> ??  
> Yes, other than that – since that uses Ruby’s PRNG, and not C’s.

> you _can_ directly call the C function using ruby/dl (see examples in  
> dist)

Thanks.

---

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [April 10, 2008, 9:17pm UTC](https://www.ruby-forum.com/t/c-rand-in-ruby/134423/5 "2008-04-10T21:17:20Z")

</div>

On Apr 10, 2008, at 12:32 PM, Joel VanderWerf wrote:

> That would call ruby’s mersenne twister, AFAIK.

yeah - i just wasn’t clear on what was being asked - ruby/dl is the  
ticket me thinks…

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [April 10, 2008, 9:00pm UTC](https://www.ruby-forum.com/t/c-rand-in-ruby/134423/6 "2008-04-10T21:00:52Z")

</div>

On Fri, Apr 11, 2008 at 03:32:09AM +0900, Joel VanderWerf wrote:

> That would call ruby’s mersenne twister, AFAIK.

Indeed – which is why I was asking about C’s rand() function.
