473,321 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

How to pick random number without repeating the same number

8
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 16439
vb5prgrmr
305 Expert 100+
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
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 Expert 512MB
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 Expert 512MB
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 Expert 512MB
Sorry, my mistake.
Here it is:
Jan 14 '13 #6
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 Expert 256MB
@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
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
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? ...
12
by: Sweety | last post by:
plz reply, thanks in advance. bye
3
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...
0
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...
1
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...
2
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
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
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.