Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random number 1

Status
Not open for further replies.

kenguru

Programmer
May 14, 2004
173
0
0
RO
Hello,

I have to generate random numbers in a given interval. I know there exist the randomize+rnd, i would something more exact, because if i start the program for two times close together, than the numbers are identical.
Any idea?
I read that there are Api functions(GetTickCount) for these, only i don't know how to include them.

Any help is appreciated.
Kenguru
 
If you are in Excel, have you looked at RANDBETWEEN worksheet function (must have the analysis toolpack add in checked)?
 

Where can i check this one? I tried looking it up in Tools/References, but no look.
Also I looked up in the help of VBA and no keyword found.
Yes i am working in excel.

Thank you,
Kenguru
 
Tools/Add ins.... Hopefully the analysis toolpack is available within the main excel side rather than the VBA Editor.
 
In VBA & Excel there are only:
Tools/ References
/Macros
/Options
/VBA project prop.
/Digital Signitures
There is a Add-Ins menu, but nothing is loaded there and it can't be loaded there.
 
Use the tools menu from Excel itself, not from the VBA editor (close the VBA editor if need be). You will then find an add-ins menu within the Spreadsheet tools menu.

Gotta go now
 
Sorry for not thinking that. I did it. But the VBA does not recognize the keyword: RANDBETWEEN.

I need to write it in VBA.

Thank You
 
What is this excel file? I don't have it on my hdd.
 
I found it. How can i work toward? I have a reference to this file in the VBA.
 
It is the addin that contains the function RANDBETWEEN. If the function works in a sheet then the addin is installed. To use it in VBA you have to set a reference to it in the VBA editor.
 
Why is this Randbetween better than Rnd+Randomize?
I have to write a code in VBA for generating this random numbers. I don't have to use in in the sheet.

Kenguru
 
Thanks For the help, i understand know.
My only problem is, after i write this program, i have to send to some one to use is. Is it possible for me to save the code, that he doesn't have to start with including these add-ins? I will send him without the excel file.
Kenguru
 
He will need to install the analysis toolpack, if he doesn't have it installed, in the tools menu of Excel.
 
Or

Code:
Public Function RandomNumber(Upper As Integer, _
     Lower As Integer) As Integer
    'Generates a Random Number BETWEEN the LOWER and UPPER values
    Randomize
    RandomNumber = Int((Upper - Lower + 1) * Rnd + Lower)
End Function

[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
Yes, I know this, only if you generate the numbers closely in time, than there will be the same.
Kenguru
 
Does your line generate the lower and upper bound? Or only between? In other words: it generates the (lower,higher) interval or [lower,higher] interval?

Thank you.
 
When you use Randomize without an argument it takes its seed as the current value of the Timer function. The timer function does not have a very high resolution being updated only 18.2 times a second on Win9x systems and somewhat faster I think on NT and later (50 or 100 I can't remember).

You could try using Randomize Timer*Rnd when your requests for new sequences of random numbers are coming in fast.

regards Hugh,
 
CBasicAsslember:

I use RandBetween (the worksheet function) fairly often and have had to program around its absence from VBA more than once.

Thanks for the tip about the aptvbaen.xls reference. I didn't know about that.

Hava
star.gif
!

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ181-2886 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top