Computing.Net > Forums > Programming > Randomly Select from DB in VB.NET

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Randomly Select from DB in VB.NET

Reply to Message Icon

Name: mking (by mkingrey)
Date: August 19, 2005 at 09:21:46 Pacific
OS: xp
CPU/Ram: xp
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Stephen Hall
Date: August 21, 2005 at 20:37:46 Pacific
Reply:

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......"


0
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Randomly Select from DB in VB.NET

Select-case problem in VB.net www.computing.net/answers/programming/selectcase-problem-in-vbnet/11240.html

call method written in VB.NET from www.computing.net/answers/programming/call-method-written-in-vbnet-from-/7816.html

Database connection in VB.NET www.computing.net/answers/programming/database-connection-in-vbnet/11398.html