Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello,
I am trying to randomly select employee names from a database. The table that I am using contains 2 fields, Name and ID. I am trying to select a random number in the range of my IDs in the table each time I click a button. For instance: if I have 10 names with ID's 1-10, I want to generate a random number 1-10, then query the database for the ID that matches the random number thus giving me a random name. Everything is in place and functional except one problem, Everytime I run the program and click the button I get the same pattern of names (or the numbers are generated in the same "random" order every time."
This is the code I used to randomize:
Rand = Int((High - Low + 1) * Rnd() + Low)
RandomNum = Rand(1, 10)does anyone know why this is using the same pattern everytime?
Any help is appreciated thanks.

mkingrey,
You need to call Randomize before calling Rnd to generate a number between 0 and 1. You could include a call to Randomize at the beginning of your program. One call is sufficient to create a new sequence every time you use the program (for all practical purposes).
On a side note, I guess you're assuming that every id in your database is used. Perhaps you might want to come up with a workaround for holes in the database's id sequence. Something kind of like a hash code methodology would work.
Stephen
"Live long and program......or at least do _something_ with all that time......"

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |