Important alert: (current site time 4/21/2014 2:48:02 AM EDT)
 

VB icon

A Non-Repeating Random Number Generator

Email
Submitted on: 5/20/1998
By: Kevin Lawrence 
Level: Not Given
User Rating: By 9 Users
Compatibility: VB 5.0, VB 6.0
Views: 45431
 
     With this simple, and very fast, routine you can generate a series of non-repeating random numbers. You can select a series of 10 numbers, or a series of a million...It doesn't matter. Can be useful for image fades, deck shuffling, random tip of the day, etc. - It even tells you how long it took to generate the series.
 
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 Non-Repeating Random Number Generator
' Description:With this simple, and very fast, routine you can generate a series of non-repeating random numbers. You can select a series of 10 numbers, or a series of a million...It doesn't matter. Can be useful for image fades, deck shuffling, random tip of the day, etc. - It even tells you how long it took to generate the series.
' By: Kevin Lawrence
'
' Returns:A popup message stating how many numbers had been mixed up and how long it took.
'
' Side Effects:The larger the series of numbers the more RAM required. Uses arrays.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=892&lngWId=1'for details.'**************************************

Private Sub Command1_Click()
'-------------------------------------------------------------
' Produces a series of X random numbers without repeating any
'-------------------------------------------------------------
'Results can be used by using array B(X)
Dim A(10000) ' Sets the maximum number to pick
Dim B(10000) ' Will be the list of new numbers (same as DIM above)
Dim Message, Message_Style, Message_Title, Response
'Set the original array
MaxNumber = 10000 ' Must equal the DIM above
For seq = 0 To MaxNumber
A(seq) = seq
Next seq
'Main Loop (mix em all up)
StartTime = Timer
Randomize (Timer)
For MainLoop = MaxNumber To 0 Step -1
ChosenNumber = Int(MainLoop * Rnd)
B(MaxNumber - MainLoop) = A(ChosenNumber)
A(ChosenNumber) = A(MainLoop)
Next MainLoop
EndTime = Timer
TotalTime = EndTime - StartTime
Message = "The sequence of " + Format(MaxNumber, "#,###,###,###") + " numbers has been" + Chr$(10)
Message = Message + "mixed up in a total of " + Format(TotalTime, "##.######") + " seconds!"
Message_Style = vbInformationOnly + vbInformation + vbDefaultButton2
Message_Title = "Sequence Generated"
Response = MsgBox(Message, Message_Style, Message_Title)
End Sub


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 Not Given 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

10/31/1999 12:53:00 AMmoccasin

'umm, isnt the above more akin to overkill?
'the following code couldve suffice

Form1_Load
Randomize timer
End Sub

command1_click
number = int(100 * Rnd + 1)
'generates random number from 1 to 100
msgbox number
end sub
(If this comment was disrespectful, please report it.)

 
10/31/1999 7:41:00 AMKevin Lawrence

I think you missed the point. Or at least the title. This code outputs NON-REPEATING random numbers.

Your code repeated within 10 numbers.
(If this comment was disrespectful, please report it.)

 
11/30/1999 8:31:00 PMmoccasin

ah ok
(If this comment was disrespectful, please report it.)

 
1/11/2000 8:21:02 AMGraeme

Use
number = Int(Rnd()*100)
Form1.Caption = number
It's easy!!!!
(If this comment was disrespectful, please report it.)

 
1/11/2000 8:23:16 AMKevin Lawrence

See my note above on 10/31/99. Your code repeats. Mine does not.
(If this comment was disrespectful, please report it.)

 
1/26/2000 10:13:25 AMLiquid Khaos

Dude, Thanks. I usually seed the rand with seconds but I feel that this is a better solution to generate my seed value. Thanks for taking the time to post. All the people who complained Obiously have never used a random number function repetativly. ;b
(If this comment was disrespectful, please report it.)

 
2/21/2000 3:37:09 PMoc

How would you make this code start at 1 to 1000 instead of 0
i've tryed Dim (1 to 1000) but i can't get it to work
(If this comment was disrespectful, please report it.)

 
2/21/2000 10:36:43 PMguy

how do i use this code
how do i get the outcome
i tried but i keep getting repeated numbers
(If this comment was disrespectful, please report it.)

 
2/28/2000 7:45:20 AMVoxal

All random numbers have to repeat some times!!
MyVar=int(rnd*100)
text1.text = Myvar
Simple!
(If this comment was disrespectful, please report it.)

 
2/28/2000 7:51:22 AMKevin Lawrence

Well, your code would repeat quite fast. But if you do it using what I like to call the 'right' way, then it won't. Pay attention to the code and maybe even try it. You'll see what I mean. It doesn't repeat.
(If this comment was disrespectful, please report it.)

 
2/29/2000 11:15:19 AMLaMar

I would like to print only five of the random numbers on a form. In your example you stated "Results can be used by using array B(X)", Is this what I use and how do I use it? Could you give me an example?
It would be very much appreciated!

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

 
4/12/2000 4:05:44 PMStephen Delisle

Kevin you rule! I was tring to think of the same thing but what I had in mind would of been a slow very slow process. thanks... I will now have to dig up that old code that I wanted that for. :o) ..
Thanks again
(If this comment was disrespectful, please report it.)

 
10/1/2001 8:01:56 PMRob Loach

Your code is good but, it would take up a huge amount of memory considering you have two variable arrays containing 10,000 slots! 3/5
(If this comment was disrespectful, please report it.)

 
11/27/2001 8:47:01 AMCharly

Great stuff!
Just what I was looking for!
(If this comment was disrespectful, please report it.)

 
2/18/2002 8:55:54 PMZanza

I need this program to give me all the sets of five digit numbers you can have using 0 - 12... im sorry for being such a newbie, but we all have to start somewhere, and i have no idea even where to start... can any of you help me?
(If this comment was disrespectful, please report it.)

 
4/11/2002 11:57:58 PMjeff

thanks! One question, how would you make only 4 digit random numbers?
(If this comment was disrespectful, please report it.)

 
1/11/2003 3:07:21 PM

>A Non-Repeating Random Number Generator<
is a contradiction in terms
suggestion:
call it a number shuffler (thats what it is)

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

 
5/12/2003 8:13:24 AMJames J. Kelly Jr.

Non repeating number:

Public Function Rnd2(Byval Min, Max as Integer)

Randomize
Rnd2 = Int((Min * Rnd) + Max)

End Function
(If this comment was disrespectful, please report it.)

 
5/12/2003 8:16:31 AMJames J. Kelly Jr.

Oops I got it backwards:

Public
Function Rnd2(Byval Min, Max as
Integer)

Randomize
Rnd2 = Int((Rnd * Max) + Min)

End Function

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

 
5/12/2003 8:38:04 AMKevin Lawrence

How does your code produce NON-REPEATING numbers?
(If this comment was disrespectful, please report it.)

 
9/21/2003 8:43:43 AM

Thanks for code. Very fast to Randomize with check the value.

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

 
1/22/2004 11:17:49 AMGuillaume GIFFARD

'Try dynamic arrays :

Dim A()
Dim B()
MaxNumber = 10000 'can be passed through arguments if you write a Sub
Redim A(1 to MaxNumber)
Redim B(1 to MaxNumber)

'Good job but this is not a Non-Repeating Random Number Generator, sorry ! It is theorically almost impossible. Read books about it !
(If this comment was disrespectful, please report it.)

 
5/24/2004 2:58:46 AM

sorry but i have to tell you that the numbers that this code generates repeat...just check it!!!
(If this comment was disrespectful, please report it.)

 
5/24/2004 9:55:20 AMKevin Lawrence

Not if you do it right.
(If this comment was disrespectful, please report it.)

 
10/30/2004 6:04:47 AM

Hey Kevin
Ur genius i was up for weeks trying to figure this type of thing out .... i was using vb cards to make a game and could not figure out how to show the cards without a repeat, but ur code works excellently... i did do one thing to it and if it is ok with you i would like you to read it over.... and in Kevins defense it does generate radom nonrepeating numbers heres the out put...26
6
49
35
17
42
4
48
22
7
15
24
51
28
20
32
9
31
44
50
46
8
30
19
25
14
16
29
18
27
45
36
41
3
13
21
2
23
12
33
43
1
52
40
5
47
37
34
38
10
39
11


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

 
10/30/2004 6:05:58 AM

now that was just a 52 varible string from my debug window of my card game
(If this comment was disrespectful, please report it.)

 
1/9/2005 8:22:21 AM

Soo Cool!
(If this comment was disrespectful, please report it.)

 
5/13/2005 7:51:11 AM

Cool...Thanks for the code!!! I've tried and tried and tried and don't get any repeating number (on the same run).

Guillaume GIFFARD is right, It will be better to use:


Dim A()
Dim B()

' Use whatever method you need to calculate your nLowerBound and
' nUpperBound (or pass them as parameters) and re-initialize your arrays...

nLowerBound = 1
nUpperBound = 10

ReDim A(nUpperBound)
ReDim B(nUpperBound)
.
.
For Seq = nLowerBound To nUpperBound
A(Seq) = Seq
Next Seq
.
.
For MainLoop = nUpperBound To nLowerBound Step -1
ChosenNumber = Int(MainLoop * Rnd + 1)
B(nUpperBound - MainLoop) = A(ChosenNumber)
A(ChosenNumber) = A(MainLoop)
Next MainLoop
.
.
Once again, tahbk you very much for the code, very usefull....
Kind Regards:
KC
(If this comment was disrespectful, please report it.)

 
5/13/2005 7:55:04 AM

By the way, Function Rnd2 does repeat the numbers....
:(

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

 
5/14/2005 4:56:13 PM

geniuos code dude, simple - but GREAT ;)!
(If this comment was disrespectful, please report it.)

 
11/23/2005 5:55:06 AMPD

Nics code but the real concept of RNG is that the same number can be allowed to appear again.Sometime if we have number 3 repeated 10 times, it might not look random, but it is random sometimes, it is just pure luck that it hit number 3 10 times. Random means anything can happen in that specific finite field as long as it does not go beyond the terms of the field. Even so, good code.
(If this comment was disrespectful, please report it.)

 
3/22/2006 2:49:00 PMLloyd

Great code, I have a competition that requires a unique random number generating. How can I output the number to a text box or lable
cheers
Lloyd
www.raffleurhouse.co.uk
(If this comment was disrespectful, please report it.)

 
4/20/2006 12:13:22 AMsub

EXCELLENT code. When properly used, it is far superior than the normal Rnd function (loading Chr() value into an array.). ***** !
(If this comment was disrespectful, please report it.)

 
10/13/2006 5:21:10 PMthe right one

ummm i think im issing sommin cause all i can git it to do is go from 0 to 20 or 20 to 0 ! no random at all
(If this comment was disrespectful, please report it.)

 
12/9/2006 9:44:08 PMmio

Very impressive and very useful... thank you!
(If this comment was disrespectful, please report it.)

 
3/6/2007 9:05:27 AMbLueStar

Use :
Randomize
Rnd()

Rnd() will NEVER repeat if you use randomize before.
If it repeat, its just luck.
(If this comment was disrespectful, please report it.)

 
11/19/2007 1:03:26 AMMatt DeKok

This would be easier to use if it was presented as a function. I converted it to JavaScript and rewrote it as a function here. Just lookup my last name "dekok" on either world to see them. Note: the VB version is actually still being processed. I just submitted it.
(If this comment was disrespectful, please report it.)

 
11/22/2007 12:09:20 AMMatt DeKok

To bLueStar, you just said "NEVER" and "if it does". Plus yes it can repeat several times after around say 50 numbers. I've done it.
(If this comment was disrespectful, please report it.)

 
9/9/2008 6:09:54 PMAxeblokie

Excellent!
Does exactly what i needed it to do!
Thanks :-)
(If this comment was disrespectful, please report it.)

 
5/20/2009 4:29:29 AMWael Sayed

Good Job...but this is not non-repeating random number generator...but simply it is numbers shuffler.

It is well-known that there is (almost) no algorithm can generate non-repeating numbers...You have to read about this issue....Good Shuffler...
(If this comment was disrespectful, please report it.)

 
7/30/2009 2:15:43 AMrushmore

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

 
10/3/2009 6:46:19 AM

NICE PROGRAM, VERY USEFUL TO OUR COMPANY
(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.