LiteSpeed Technologies
Download Download     Blog Blog     Wiki Wiki     Forum Forum     Store     Contact Contact    

Go Back   LiteSpeed Support Forums > External Applications > Ruby/Rails > Problem with sort_by{rand}

Reply
 
Thread Tools Display Modes
  #1  
Old 05-14-2009, 01:54 PM
ideaoforder ideaoforder is offline
Member
 
Join Date: Jul 2008
Posts: 28
Default Problem with sort_by{rand}

I'm running a RoR survey application that randomized question/answer order on a page using something like: answers.sort_by{rand}

This happens on several pages, each generating its own random number (in the view).

This sets the order on page render. It works fine on my local machine in both production and development mode running Mongrel, but doesn't work on my production server running Litespeed 4.0.3 (or 3.3 before that).

Strangely, when I restart the server, the value of rand appears to change. But it's the same for every page.

So it seems like this is some sort of caching problem where rand is only getting generated once.

Any ideas?

Thanks!
Reply With Quote
  #2  
Old 05-14-2009, 02:08 PM
mistwang mistwang is offline
LiteSpeed Staff
 
Join Date: May 2003
Location: New Jersey
Posts: 7,590
I think it is the rand() function in Rails, the random generator has been initialized only once when the Rails Application starts, then RailsRunner for LSWS will fork() from the parent process, to process a request, then exit if idle for a while, then fork() another process when a new request comes in a while later. The random number generator has the same state for both children processes, so they produce the same random number.

The solution is to initialize the rondom number generator again before calling rand() function. In C it is srand() function, not sure what it is in Ruby.
Reply With Quote
  #3  
Old 05-14-2009, 03:04 PM
mistwang mistwang is offline
LiteSpeed Staff
 
Join Date: May 2003
Location: New Jersey
Posts: 7,590
This problem has been confirmed in our lab. released ruby-lsapi 3.5 to reinitialize the random generator automatically.
Reply With Quote
  #4  
Old 05-15-2009, 01:24 PM
ideaoforder ideaoforder is offline
Member
 
Join Date: Jul 2008
Posts: 28
Default Perfect

yup--the function is srand() in Ruby too. Worked like a charm. Thanks!
Reply With Quote
  #5  
Old 05-15-2009, 02:20 PM
mistwang mistwang is offline
LiteSpeed Staff
 
Join Date: May 2003
Location: New Jersey
Posts: 7,590
if you upgrade to ruby-lsapi 3.5, you do not need to call srand()
Reply With Quote
Reply

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 01:37 PM.



- Archive - Top
© Copyright 2003-2011 LiteSpeed Technologies, Inc. All rights reserved. Privacy Policy.