Random sort not random?

I’ve built a backend function that creates a list and populates it with five random articles from my Articles datatype. This is the logic I’ve used so far.
Screenshot 2024-06-26 at 10.28.21 PM

Each time I run the workflow, it returns the same list in the same order.

I’ve tried adding in a second random sort into the function as shown here, and tried “breaking” the cache through a “id <> random_value" condition in the search as described here. Neither worked.

Is this a bug? Am I missing something? If it’s a bug, is there a workaround?

What are the constraints on your Search for Articles?

How many articles are in your database?

@boston85719 There are currently 40-50 articles in the database, but there will be hundreds if not thousands, once the site is live. I’ve run the function 30+ times, and it produces exactly the same output in my database (screenshot below).
Screenshot 2024-06-27 at 12.32.35 AM

I’ve tried adding a few different constraints to see if that addressed the issue (including random sorting, id <> random value, sort by created date, etc.) with no luck.

Show the search you perform in the backend workflow with constraints used that are getting the same result every time…something might be wrong with it.

It’s in the original post, but here it is.
Screenshot 2024-06-27 at 12.47.48 PM

I’ve tried removing the “random sorting” constraint (just using the sort function) and removing the “sorted” function (just using the random constraint), and they both return the same list.

The oddest thing about this is the random item functionality works fine. It’s only random sorting that returns the same variables.

Are you refreshing the page each time? Notice the reference: Search | Bubble Docs

Random sorting

Choose this option to randomize the order results. It will be different for each user and after each refresh. It will be the same when re-running a workflow without refreshing the page.

1 Like

@J805 Yes, I’ve refreshed the page each time, tried logging out and back in, and even changing the user activating the workflow (which does give one new random sort, but then all subsequent random sorts for that user are exactly the same).

When I try random sorting on this list it seems to work. It does say it only works for lists of 50,000 and less.

Editor: testApp42wCleanDB | Bubble Editor
Preview: https://testapp42wcleandb.bubbleapps.io/version-test/random?debug_mode=true

Each time I refresh the page it seems to give me a new order of the FAQs.

Does it work for you too?

@J805 No, it displays the articles in this order each time I refresh it. I wonder if its a browser caching issue…


e

Yeah, maybe. What browser are you using? I am on a Mac and tested with chrome and safari and it works. :man_shrugging:

@J805 Using Arc, just tried with Chrome + new user and it’s generated exactly the same list once again lol. Is there any difference in the search functionality you’re using?
Screenshot 2024-06-27 at 12.47.48 PM

Alternatively, since random item does work, do you have any idea how to build a workaround using Do Search For:random item, and then run that 5 times to create the list?

I don’t really see a difference, I just don’t have the items until #5. Maybe that is the issue? Maybe Bubble is doing something weird with that?

It has to be some sort of browser issue though since it works for me and not for you with the same page.

What happens if you remove the ‘items until #5’? I am curious if it is pulling the first 5 before any ‘random sorting’ happens…

@J805 When I opened your preview in Safari, the FAQ list sorted randomly in your preview. However, when I activated my workflow in safari, it generated the same list…

I tried removing items until 5, and it was still the same list order, but including all articles instead of 5. :frowning:

1 Like

@J805 When I originally tried building this functionality, I tried using Search for Article: random item, and that worked (however it only created one article). Is there a workaround where I could run the random item function 5 times, and add those to the same list?

I know that would skyrocket my WU, but I’m feeling pretty desperate at this point…

Well, yeah. The WUs would be very high. You can just do that step 5 times. You said it worked once, you could technically just do the same step five times adding the results to the list at the end of the workflow. Does that make sense?

@J805 I guess it’ll have to do. Once live, the workflow will only run once per day, so hopefully, it won’t be an issue. By the way, thank you so much for going back and forth with me on this. It means a lot, truly.

I’ve started a new workflow, which allows me to populate two truly random articles.

Step 1.
Screenshot 2024-06-27 at 2.53.54 PM

Step 2. (adding in constraints to ensure it doesn’t choose the same article twice.
Screenshot 2024-06-27 at 2.54.04 PM
d

I think I’m missing some logic in step 2. Whenever I try and add new logic to add a 3rd article (either in step 2 or in a new step), the third article replaces the second instead of adding to the list. Any thoughts?

Ok, here is the example I gave before but changed to the really complicated way. This provides the random list. Does it work for you when you press it and then refresh the page and press it again?

Editor: testApp42wCleanDB | Bubble Editor
Preview: https://testapp42wcleandb.bubbleapps.io/version-test/random?debug_mode=true

you are not showing the search constraints, just the dynamic expression. Show the search constraints on the search itself.

@J805 Yes, that works!

But unless I’m mistaken, setting the custom state as you did only works on the front end. Right now, I’m using a button on the front end to test the workflow, but once I confirm the workflow creates a random list, the live version will be a scheduled recurring event that runs on the backend once per day.

As a result, I don’t think custom states will work for me :frowning:

@boston85719 I’m a bit lost here. Maybe I’m misunderstanding your terminology as I’m new to Bubble.

In the screenshot you replied to, I believe the dynamic expression is the “create new article” box on the left, and the search constraints are the “Search for Articles” box on the right. As you can see, there are no constraints in the Search for Articles (outside of Sort by Random Sorting, which I’ve tried removing but didn’t solve the issue with random sort).

Are you referring to something else? The screenshot I showed you is the entire workflow. It’s only one step.