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.

Code Snippets / 3 "rand" functions

Author
Message
Underworld 1020
20
Years of Service
User Offline
Joined: 2nd Mar 2004
Location: NY, USA
Posted: 8th May 2006 20:03 Edited at: 11th May 2006 01:57
EDIT: I've re-done this post yet again...

NOTE: Works for both DBC and DBPro!

Let's say you need to get a random integer from 5 to 20.
Normally in DB you would need to do something like this:


However if you need to do that sort code in several places throughout your program, you may find it easier just to use my 'randInt' function:


Here's the full source dump for that example:


NOTE: The randInt function returns only integer values.

Now that's nice, but what if you need to get a random number thats not in a certain range? Well, then you use my 'randStringFromFile' function. This function opens up an file and returns a line in the file, by random. Now this function returns a string value, so the converting to either an interger or float, etc is left up to you.

The 'randStringFromFile' function:


Now in order to use this function you need to tell it first what file to look in and then how many lines are in that file. To find how many actual lines are in that file you use my 'totalLinesInFile' function:


NOTE: This function simply opens up a file and returns the total amount of lines in that file as an integer value.

Now let's put these functions to use! Let's say you want to pick one of the following values by random: 10, 20, 35, 50.1, 8999.125. Now neither
the DB rnd() command nor my 'randInt' function can do this, so this is my way of accomplishing this task. First put all the values in a file, I'll use "randNumbers.txt".

Like this:


NOTE: since the 50.1 value is listed more than once it will be more likely to be picked than all the other values.

Now that we have all our values in the "randNumbers.txt" file, we can now figure out how many lines or numbers are actually in that file. So we use the 'totalLinesInFile' function.

Like this:


Now that we have how many lines are in the file stored in the 'totalLines' integer varible, we can now actually use the 'randStringFromFile' function.

Like this:


Here's the full source dump for that example:


NOTE: Make sure you have all your values in the file, and on a seperate line in the specified file and also on blank lines.
NOTE: To get a more randomized result, you can put this at the top of your code:


If you have any questions/comments, please let me know

TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 9th May 2006 12:52
Quote: "EDIT: I've re-done this whole post and the functions, to make things more clear and the functions faster."


Unfortunately, you haven't - made it clearer that is. I may be getting a little slow in my old age, but I'm not sure what you are trying to achieve here.

The idea of creating functions is to accomplish a task which would otherwise be more difficult without the function. Your functions seem to be making more work.

Your last code snippet has the line:

print randNumInFile("randomNumbers.txt", totalValues)

...which calls a function which doesn't exist! Have you even run it yourself?

You also didn't state whether it was for DBC or DBP (of particular importance if it won't run in one or the other like your code). But, most people don't for some reason, so you are not on your own...

Anyway, your randNum function receives and uses floats, yet in the call it passes integers?

The program won't even run in DBC - which is correct. It shouldn't!

What I don't understand is why it does run in DBP (the version I have at least) - when it shouldn't.

Besides that, if you want to make your function faster, then you shouldn't be using floats at all - they are much slower than integers.

All that aside, the Rnd function doesn't return floats anyway - only integers.

TDK_Man

Underworld 1020
20
Years of Service
User Offline
Joined: 2nd Mar 2004
Location: NY, USA
Posted: 11th May 2006 02:01 Edited at: 19th Jul 2006 14:09
I fixed up my first post again...and I think everything is good now. I've tried it in both DBC and DBPro; it works for both. I've also changed the functions themselves, so I think you'll be happy now, but if you aren't I really don't give a crap

Also here's the functions in use:
http://forum.thegamecreators.com/?m=forum_view&t=71162&b=6&p=0


Check Out My Site: http://www.freewebs.com/underworld1020 - UNDER CONSTRUCTION!

Login to post a reply

Server time is: 2024-11-23 07:52:17
Your offset time is: 2024-11-23 07:52:17