Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7490239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:34:11+00:00 2026-05-29T15:34:11+00:00

I am trying to assign a defined random order to a table of over

  • 0

I am trying to assign a defined random order to a table of over 10000+ records. I have a function utilizing a start date and adding 1 second to each consecutive date, assigned randomly. Then I could sort by the random assigned date. My function worked fine with 50 records, but fails with 10000+ records.

It sets correct dates for about 9000 records, but 1146 records get assigned 0 (1969-12-31 19:00:00) Any help getting this or something similar to work would be appreciated.

function randomize(){
  $count = $this->Application->find('count');
  $order = range(0, $count-1); // Array of numbers 0 to count-1
  $startDate = strtotime('December 13, 2011 0:00:00');
  shuffle($order); // scramble array of numbers
  $Applications = $this->Application->find('all');
  set_time_limit(0);
  foreach($Applications as $app){
    $this->Application->id = $app['Application']['id'];
    $this->Application->saveField('order', date('Y-m-d H:i:s', $startDate + $order[$this->Application->id]));
  }
  set_time_limit(30);
}

Update: I am using MySQL database but need a permanent state for 1 randomization, not repeated randomization as per ORDER BY RAND(). I also updated the code (see above) to reduce overhead, and increased memory in php.ini from 128M to 256M. With the code change the bad dates are no longer 0 but the same as $startDate indicating it may be an issue with the $order array of numbers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:34:11+00:00Added an answer on May 29, 2026 at 3:34 pm

    Questions:

    Are you sure you’re using the proper date format there?

    Why a start date to randomize? Take the date as a fixed number, as you’re doing (X in this case): If you do X + givenOrderNumber for each record then the order will be defined by givenOrderNumber… so why the unnecessary addition?

    I’ve got the query I understand you’re looking for here:

    set @num = 0;
    select *,
      date_add('2011-12-13 00:00:00', interval @num := @num + 1 second) as newOrder
    from table1
    order by newOrder
    

    Example

    It sorts the records by a date which is incremented by one each time. Now if you want to use the application id cheat:

    select *,
      date_add('2011-12-13 00:00:00', interval id second) as newOrder
    from table1
    order by newOrder
    

    Example

    However, whether this is useful or not for you… it seems to be unnecessary.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to assign value to an object array that is defined
I'm trying to assign values contained in a lookup table to multiple variables by
i have a menu item, and i'm trying to assign its OnClick event handler:
I have a few properties defined in my header file like so @property (assign)
I have an NSUInteger defined as a property like so: @property (nonatomic, assign) NSUInteger
I'm trying to build a function to assign VARIANTARG types to a DISPPARAMS structure,
I'm trying to write a function which re-uses the implicit conversions which I have
I'm trying to use a user-defined function in XSLT that repeatedly calls the value
So i'm trying to assign a hyperlink to a single word in a {p}
I am trying to assign objects in an NSMutableArray using this code - (IBAction)variablePressed:(UIButton

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.