Important alert: (current site time 7/16/2013 1:40:50 AM EDT)
 

VB icon

A better random number

Email
Submitted on: 3/11/2003 9:58:18 PM
By: Brett Johnson  
Level: Intermediate
User Rating: By 5 Users
Compatibility: VB 5.0, VB 6.0
Views: 28398
 
     The random number function in VB produces the same number every time you restart an application if the conditions are the same. To fix this I use time.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: A better random number
' Description:The random number function in VB produces the same number every time you restart an application if the conditions are the same. To fix this I use time.
' By: Brett Johnson
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=43944&lngWId=1'for details.'**************************************

Dim intX as Integer
Dim intTime as Integer
dim intRandomNum as Integer
' uses time to get a better random #
intTime = Hour(Time) + Minute(Time) + Second(Time)
For intX = 1 To intTime
intRandomNum = Int(Rnd)
Next intX


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

3/11/2003 10:09:19 PMChloe

May want to read about the Randomize function and how one should call it before using the Rnd function.
(If this comment was disrespectful, please report it.)

 
3/11/2003 11:19:18 PMBattleStorm

Just use this function. It Randomizes the first call then again every 1000 calls thereafter: (Please excuse the formatting)

Private Function Random(Min As Long, Max As Long) As Long
Static RandControl As Integer

RandControl = RandControl - 1
If RandControl < 0 Then
Randomize Timer
RandControl = 999
End If
Random = Int(Rnd * ((Max + 1) - Min)) + Min
End Function
(If this comment was disrespectful, please report it.)

 
3/12/2003 1:13:34 PMNelson Oliveira

randomize timer

works faster, just put it before the rnd(num)
(If this comment was disrespectful, please report it.)

 
3/13/2003 2:26:36 AMRiggs_VB

The easiest way is:
Dim num1
Randomize
number = Int((100 * Rnd) + 1)

This is 4 a random number between 1 and 100
Simple hey...
(If this comment was disrespectful, please report it.)

 
3/13/2003 10:28:03 AMBattleStorm

No simple is just copying the function I have defined above then just calling it using "number = Random(1,100)", which will generate a random number between 1 and 100 with a new seed every 1000 calls.
(If this comment was disrespectful, please report it.)

 
4/21/2003 2:37:48 PMSean Ranjan Dhillon

Everyone seems to use different methods, mine is also based on time:
Int(Rnd * (Second(Time) + Minute(Time)) / Int(10))

It churns out 12 possibilities, but that's as many as I nedded at that time, anyway, you could change the number, "Int()" always sets the result as a whole number.
(If this comment was disrespectful, please report it.)

 
5/12/2003 7:51:53 AMJames J. Kelly Jr.

Most C programmers use the system time to generate a random number.
(If this comment was disrespectful, please report it.)

 
9/25/2003 8:30:49 PMFlash

Uhhh, guys You can do it a lot simpler than that. VB already has a function built in to do this. try putting this in

Randomize Timer
var = Int(Rnd * 1000) + 1

Thats a random number between 1 and 1000 and its random every time. :)
(If this comment was disrespectful, please report it.)

 
7/25/2004 2:01:34 AMRui Pedro Nascimento Matos Manso

Flash ure the last but not the least comment. I think u re totally right about youre code: short but clean and functional. :)
(If this comment was disrespectful, please report it.)

 
8/31/2004 11:26:06 AM

all you do is put this:

Randomize:
rand = int(num * rnd)

its simple !!
(If this comment was disrespectful, please report it.)

 
12/29/2004 4:57:10 PM

nice programme code with data base
(If this comment was disrespectful, please report it.)

 
6/17/2005 12:43:42 PMPatabugen-

you people dont get it, the whole idea of this function is because the "super duper simple random number" isnt as random as it seems, as it says in the description if the conditions are the same it will give you the same "randomm" number every time. So the simple one may well be good enough for you (as it is for me) but sometimes you need a more random number and so this function does that.
(If this comment was disrespectful, please report it.)

 
6/21/2005 9:37:56 AMthe Mussel

Easier method is to do this at program start to set the seed value:

DIM v as Single
v=Rnd(-Timer)

Now just call normally:

RandomVal = Rnd

You will get a perfect new randomized value each time program is restarted and every time after that.
(If this comment was disrespectful, please report it.)

 
6/21/2005 9:59:58 AMBgh251f2692001

just use
RANDOMIZE TIMER
a = int(rnd * 10)+1
'gives you a number from 1 to 10
(If this comment was disrespectful, please report it.)

 
6/21/2005 10:58:09 AMRaymond van Bommel

A number on time isn't really random, since it is possible to predict te number...
(If this comment was disrespectful, please report it.)

 
6/9/2006 5:29:54 AMsaypen

surely it would be easier to use the randomize function?
X = randomize(hour(time) + minute(time) +(second)time
(If this comment was disrespectful, please report it.)

 
5/3/2007 6:12:52 AMadel

Thanks for source code...
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.