Trouble with Random number exclusions (1 Viewer)

rikklaney1

Registered User.
Local time
Today, 13:51
Joined
Nov 20, 2014
Messages
157
So I'm stuck with something that should probably be easy but I can't figure it out. I want to open a random picture named 1 to 50 once an hour. Easy enough, but I need to not use the same picture more than once in 24 hours. When I open the picture I update a record in the table of the last time it was opened. I have a listbox showing me all the pictures used in the last 24 hours by their number. Now how can I generate a random number between 1 and 50 that is not in the listbox?
 

plog

Banishment Pending
Local time
Today, 15:51
Joined
May 11, 2011
Messages
11,611
Now how can I generate a random number between 1 and 50 that is not in the listbox?

You can't. But I have 2 methods that will accomplish what you want:

1. Generate random numbers until you find one that is valid to use. You would use a While loop and inside it you would generate a random number, test if its valid and if valid use it and exit the while loop. If not valid, keep looping.

2. Populate your Use table beforehand. You would have a function that takes the ID's of your records, puts them in an array, then shuffles the array. Next you would loop through that array and INSERT records into your Use table putting the ID and the next time into the table. Then you just read that table when needed and it pulls the next record and you use that to display your images.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 16:51
Joined
May 21, 2018
Messages
8,463
The other simple way would be to make a table with picture names in it. In your case the names are 1 to 50. Then make a query with a random number column and sort by that column. Open in order. Once you get to the last record requery and get a new set of numbers. If used in a query ensure you pass the record ID as a seed into the Rnd function. If not you will only get one value.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:51
Joined
May 7, 2009
Messages
19,169
you create a function and call it, pass your listbox:

public function getRndPic(byref lst as listbox) as integer
dim i as integer
on error resume next
i=random_num()
do while True
lst=i
if err.number<>0 then exit do
i=random_num()
loop
getRndPic=i
end function


public function random_num()

'Initialize the random number generator
'=> Randomize : add this before you call the Rnd function to obtain completely random values
Randomize

'Random whole number between 1 and 50 :
random_number = Int(50 * Rnd) + 1



End function
 

rikklaney1

Registered User.
Local time
Today, 13:51
Joined
Nov 20, 2014
Messages
157
A loop. I knew there was an easy solution I just wasn't thinking of. Thanks.
 

Users who are viewing this thread

Top Bottom