Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Random + DATA

Author
Message
Raven maker
17
Years of Service
User Offline
Joined: 15th Dec 2006
Location:
Posted: 15th Dec 2006 16:16
Hello all,

I've searched through the forums but I was unable to find the answer to this:

If I create a DATA set is their anyway to select an entry from it at random? By that I mean if I have:

CARS:
DATA 3
DATA "Car", 100, 10, 10
DATA "Bus", 60, 5, 15
DATA "Race Car", 150, 25, 2

What kind of code could I use to select one of these at random? Or am I going about this the wrong way? Part of me thinks I should be using a combo of functions and RND for this but I really have no clue.

Thanks for any help with this.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 15th Dec 2006 20:15
All you need to do is pick a random number between 1 and 3, then read that many sets of items - the last set read will be the one you want.



Another way to do this would be to put the items into an array first to save reading through the items - you'll get a big speed gain that way when accessing items in a large list.



A little more code, but the array will be ready for immediate use every time you need it

TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 15th Dec 2006 22:48 Edited at: 15th Dec 2006 22:49
Or there's the 'usual way'...



Just typed this in from memory so excuse any errors in the Select block - I'm still in Delphi mode at the moment so I might be a little mixed up!

TDK_Man

Raven maker
17
Years of Service
User Offline
Joined: 15th Dec 2006
Location:
Posted: 18th Dec 2006 19:48
Thank you to both of you, I actually went with Option #2, it made more sense to me. =)

Login to post a reply

Server time is: 2024-09-25 13:27:20
Your offset time is: 2024-09-25 13:27:20