473,606 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pick random number without repeating the same number

8 New Member
I am using a timer to call a random number range from 1 to 50 and display it in a label. How can i display the number without repeating the number that have been call out?

For example, if number 10 is call out and display in the label. How can i make sure that number 10 wont be call out and display again?
May 8 '10 #1
7 16476
vb5prgrmr
305 Recognized Expert Contributor
Use a collection with both key and text being a number from 1 to 50. Use Rnd with collectionname. count + 1 to select one of those numbers and retrieve the text. Then remove that item from the collection and thus it will never be called again... as an example
Expand|Select|Wrap|Line Numbers
  1. Dim C As New Collection, I As Integer
  2. For I = 1 To 50
  3.   C.Add CStr(I), CStr(I)
  4. Next I
  5. I = Int(Rnd * C.Count + 1)
  6. MsgBox C.Item(I)
  7. C.Remove (I)
  8. MsgBox C.Item(I)
  9.  
as you can see from the second message box, a different number appears because the original number has been removed. Don't forget, use the collectionname. count to create your random number so you don't go out of bounds and don't forget to add a check to see if the collection is empty (Count = 0) and if it is to reload the collection with new numbers...

Edit: Also: Use the Randomize statement in form load so it is called only once...

Good Luck
May 8 '10 #2
Suprim
8 New Member
Sorry, i try that but the numbers is repeating times and again...it really doesn't work but it works for choosing the random numbers only...if any other code are there then please post immediately as soon as possible...but thank you for the above code...
May 8 '10 #3
patjones
931 Recognized Expert Contributor
Hi,

I did this in Access VBA (using vb5prgrmr's concept) and it works nicely. It should work essentially the same if you are using Visual Studio or some other environment.

Expand|Select|Wrap|Line Numbers
  1. Public C As New Collection
  2.  
  3. Private Sub Form_Open(Cancel As Integer)
  4.  
  5. For j = 1 To 50
  6.   C.Add CStr(j), CStr(j)
  7. Next j
  8.  
  9. Randomize
  10.  
  11. lstRandoms.RowSourceType = "Value List"
  12. lstRandoms.RowSource = ""
  13.  
  14. End Sub
  15.  
  16. Private Sub Form_Timer()
  17.  
  18. Dim k As Integer
  19.  
  20. If C.Count >= 1 Then
  21.     k = Int(Rnd * C.Count + 1)
  22.     lstRandoms.AddItem C(k)
  23.     C.Remove (k)
  24. End If
  25.  
  26. End Sub

Here lstRandoms is the name of my list box control. I set my timer interval at 1000 (1 sec) so that I was not waiting around forever for the sequence to complete, but you might need to run it slower.

Pat
May 8 '10 #4
Guido Geurs
767 Recognized Expert Contributor
dear,


I hope this will help (see attachment)
It places the already Randomized numbers in an array and checks the new random with the values in the array.
The list is only for visual checking !
I have only RND from 1 to 10 for fast testing !!

br,
May 9 '10 #5
Guido Geurs
767 Recognized Expert Contributor
Sorry, my mistake.
Here it is:
Jan 14 '13 #6
Rodney Roe
61 New Member
vb5prgmr your snippet of code is awsome! I was previously doing the same thing with arrays but this cut my code in half. I havn't played with collections very much so thanks for your insight.
Jan 14 '13 #7
CyberSoftHari
487 Recognized Expert Contributor
@suprim: This is not a place to do your homework. You have to try then post your question. Mod. should close this thread (almost done).
Jan 15 '13 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
426
by: Job Lot | last post by:
how to genrate random colors without repeat of the same color or color code. I am creating a line graph using ComponentOne Chart control and what to show series with different colors. thanx
7
7261
by: eric.gagnon | last post by:
In a program randomly generating 10 000 000 alphanumeric codes of 16 characters in length (Ex.: "ZAZAZAZAZAZAZ156"), what would be an efficient way to ensure that I do not generate duplicates? STL set, map? Could you give me a little code example? Thank you.
12
5007
by: Sweety | last post by:
plz reply, thanks in advance. bye
3
2162
by: Danny Tuppeny | last post by:
Hi all, Is there a way in something like a DataList, to have a different colour background (via CssClass) without repeating all my code in both the ItemTemplate and AlternatingItemTemplate blocks? I have quite a lot of stuff in the ItemTemplate block, but because so much of it is bound fields, creating a seperate user control mapping all of these fields across would be a nightmare to maintain. Instead, I'd like something like:
0
1337
by: joaoduraes | last post by:
Hello... I have a Mysql DB with X entries. Each one displays an item to the user. How can i create a script to show all items in a random way (one at a time) without repeating.And once all itens have been show, to receive an alert. Thanks.
1
1782
by: gobblegob | last post by:
Hi guys i am trying to randomize 6 numbers into 6 textboxes up to the value of 60 without repeating a number but i dont know how an error handler would work for duplicate numbers any idea's? this is what i have so far: Text1.Text = Int(Rnd * 60) + 1 Text2.Text = Int(Rnd * 60) + 1 Text3.Text = Int(Rnd * 60) + 1 Text4.Text = Int(Rnd * 60) + 1 Text5.Text = Int(Rnd * 60) + 1 Text6.Text = Int(Rnd * 60) + 1
2
4794
by: EzeeGoN | last post by:
Using Flash8 Actionscript 2.0 i am looking for a method to generate sequential numbers in a random order (not using the same number twice) can anyone help please?
20
5100
by: A | last post by:
Hi all. Is this a bug or what??? here is a simple code: <?php mt_srand(1); echo mt_rand(0, 255)."<br />"; echo mt_rand(0, 255)."<br />";
15
13188
by: capablanca | last post by:
Hi How to create a program that ask to the user enter 10 numbers and the program can form all the combinations of 5 numbers without repeating. Thanks for any advice.
0
8009
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7939
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8078
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8299
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5962
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3964
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2442
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1548
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1285
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.