Skip to content

Ozar.net Developer Blog

Programming in PHP, C, C#, ASP.NET, Java, Objective-C, SQL also for MS SQL Server, Oracle & MySQL Development

Menu
  • Home
  • About
  • Tutorials
  • Contact
  • Privacy
Menu

How to generate a random number sequence that is non-repeating in visual basic .net ?

Posted on April 8, 2010April 21, 2010 by OD

I am writing a matching program, and need 24 objects to randomly appear, each twice, across 48 spaces. I figured the best way for me to do this would be to assign the positions a number 1 through 48. but the problem is I can’t find a way to generate a random number sequence in visual basic 9 .net

I found many examples in visual basic .net but the conversion doesn’t work. If not some code, many some example programs that either use a random number sequence or randomly sets the position of objects.

Thanks.

2 thoughts on “How to generate a random number sequence that is non-repeating in visual basic .net ?”

  1. John says:
    April 8, 2010 at 12:36 PM

    in the system.random namespace:

    Dim rnd As New Random
    Dim i As Integer
    For i = 1 To 10
    Console.WriteLine(rnd.Next())
    Next

    please note that random numbers aren’t “unique”, so you need to add something more to it. how about you fill your 48 spots with the objects … and then run a a loop (like 100 iterations) to exchange object positions by using two random generated indexes .. after that your field of 48 objects will be pretty random!

    Log in to Reply
  2. Chie says:
    April 8, 2010 at 1:00 PM

    You can do a simple shuffling algorithm, see Wikipedia.

    http://en.wikipedia.org/wiki/Shuffling

    Personally the algorithm I like to implement is this: to populate a list with a list of items, then transfer them one by one randomly into another list.

    But hey, here’s the simplest general purpose shuffling algorithm, which I implemented in VB:

    _
    Public Function Shuffle(Of T)(ByVal items As IEnumerable(Of T)) As IEnumerable(Of T)

    Dim x = items.ToArray()
    Dim rand As New Random()
    If items.Count <= 1 Then Return x For i = 0 To items.Count - 2 Dim index = rand.Next(i + 1, items.Count) Dim tempItem = x(i) x(i) = x(index) x(index) = tempItem Next Return x End Function

    Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • NSTableView with Core Data Tutorial using NSFetchedResultsController
  • CodeIgniter Wizard – a new PHP CRUD Code Generator for macOS
  • Installing Oracle Database 10g Express Edition and Changing the Default HTTP Port
  • Xcode Tutorial 2 – Putting iAd into your App (The Apple Approved Way)
  • Cocoa Programming: A Quick-Start Guide for Developers

Blogroll

  • All Systems GO
  • GOZmosis
  • Maclord's Blog
  • Ozar.net

Tags

.NET 2008 application ASP.NET basic Beginning best books business Business Objects C# Database Databases Development Edition Guide iPhone Java Java EE learn Learning Microsoft MS SQL Server MySQL Objective-C Programming objects Oracle Part PHP PL/SQL Professional Programming Server Software SQL T-SQL TSQL Tutorial Tutorials Using Video Visual Visual Basic .NET Web Programming Tutorials XCode
© 2026 Ozar.net Developer Blog | Powered by Minimalist Blog WordPress Theme

Powered by
►
Necessary cookies enable essential site features like secure log-ins and consent preference adjustments. They do not store personal data.
None
►
Functional cookies support features like content sharing on social media, collecting feedback, and enabling third-party tools.
None
►
Analytical cookies track visitor interactions, providing insights on metrics like visitor count, bounce rate, and traffic sources.
None
►
Advertisement cookies deliver personalized ads based on your previous visits and analyze the effectiveness of ad campaigns.
None
►
Unclassified cookies are cookies that we are in the process of classifying, together with the providers of individual cookies.
None
Powered by