Random from 1 to 500 without repeating

Asked

Viewed 240 times

3

I have a table in the database with ids from 1 to 500, each time the page is updated a number drawn appears and I make a select and display a message that is in the selected id, but I would like each updated the numbers do not repeat until I arrive at the last and so reset and start again.

Could you do it just with javascript? or will I have to use php?

  • Get it, search for "Fisher-Yates" right here on the site for the algorithm. Basically you store an array in the browser (cookies, localstorage etc). If you do it on the PHP side, with Session or similar mechanism is better. Just "shuffle" the numbers and an array, and redo the draw only after using all (do not need to touch the DB for this). It would be important for you to [Edit] your post and put the code you are using as the basis, the structure of the DB or array used, etc., to serve as the basis for the answers. - PHP-side function ready http://php.net/manual/en/function.shuffle.php

  • @Bacco saw a post about you more I found complicated rsrsrs because in my case I just want one result at a time and somehow I believe q some time the function can be slow due to the number of records, vo after the one analyzed calmly, thanks for the help.

  • 500 numbers is a very low value. Just vc do an array and $drawn = shuffle( $original ). Ai will take one by one of the array until it is over (and only draw again when the array is over). Fooling around with eight lines of code does the whole thing. Remembering that just draw the numbers, the rest takes the DB according to the current number only when using - if (Count($drawn)==0) { generate and shuffle }; removes the 1st of the array and uses. Save back in session; - basically this solves everything

  • I understood, but the trend is this number grow, anyway gave me to understand your logic and I will do some tests here too, thanks.

1 answer

2


You can add to this table the column TINY INT (1), of for example name usado and at search time do search limited to one with the condition used 0 ,

For example:

SELECT column 
FROM table
WHERE usado = 0
ORDER BY RAND()
LIMIT 1

in this query I get a random record limited to one that has not yet been used,

After picking up this record you update the same to usado = 1

For example:

UPDATE table SET usado=1 WHERE id=aqui_seria_o_id_recebido_na_busca_anterior

When running the first query leave a condition in case the search does not return anything, it runs an update to update the whole table and so run the select again.

Example:

UPDATE table SET usado=0

In case I’m simply updating the entire table to the status usado = 0.

All these changes would be on the server side, so you wouldn’t need to change html or javascript, but of course that’s just my idea. I hope I’ve helped.

  • got it, I had been thinking about the possibility of the update more without Rand, it is already a way although gave to prefer a form that did not depend on the update, anyway it is already a solution, thanks ;)

  • another @Bulfaitelo question, at the end when the options were over, how could I reset all the records to 0 at once? what is the best option? thanks.

  • @Patriquealves Necessarily you need some way to know which have already been used, so the update, with respect to the second question I will edit and put this update.

  • super understood, gave to clear enough here and I’ve done the test, helped a lot, thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.