Randomizing students in class

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

42 Replies - 2115 Views - Last Post: 31 October 2013 - 11:55 AM Rate Topic: -----

#1 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Randomizing students in class

Posted 08 October 2013 - 07:45 PM

So,I'm making a program that has the objective of randomizing sits on a class. Lets imagine there's a room with 32 sits, 4 rows with 8 chairs in each row.Two classes (1001 and 2001 lets say) arrive with a a total os 32 students, they should be randomized in a way where the first row only contains students from 1001, row two with students from 2001, row three with students from 1001 and row four with students from 2001, so there are no "touching" rows with students from the same class. Inside the rows the students from the class should be randomised. I have attached to pictues, one is an output exemple and the other is a blank model, for the program to use. My main doubt is how to get the program to actually write that info down to the image, and maybe output it as a .jpg . I'll make more research and post my advances and doubts, any help would be very welcome. thanks.
Attached Image


Attached Image

Code with some notes:
Option Strict On
Imports System.Windows.Forms.PaintEventArgs
Public Class Main


    Private Sub Pan_Paint(sender As Object, e As PaintEventArgs)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Yloop As Int32 = 1 To 3 Step 1
            For Cloop As Int32 = 1 To 5 Step 1
                TC1.Items.Add(String.Format("{0}00{1}", Yloop, Cloop))
                TC2.Items.Add(String.Format("{0}00{1}", Yloop, Cloop))
            Next
        Next
        TC1.SelectedItem = "1001"
        TC2.SelectedItem = "2001"
        For Anloop As Int32 = 0 To 32 Step 1
            AC1.Items.Add(Anloop)
            AC2.Items.Add(Anloop)
        Next
        AC1.SelectedItem = 16
        AC2.SelectedItem = 16
    End Sub

    Private Sub Go_Click(sender As Object, e As EventArgs) Handles Go.Click
        'Strings with the classes names
        Dim C1 As String = TC1.SelectedItem.ToString
        Dim C2 As String = TC2.SelectedItem.ToString
        'Ints with the numbers of students
        Dim A1(CInt(AC1.SelectedItem)) As Int32
        Dim A2(CInt(AC2.SelectedItem)) As Int32
        For Al1 As Int32 = 0 To (CInt(AC1.SelectedItem) - 1)
            A1(Al1) = Al1 + 1
        Next
        For Al2 As Int32 = 0 To (CInt(AC2.SelectedItem) - 1)
            A2(Al2) = Al2 + 1
        Next
        'Rows, 1 space in array per chair
        Dim R1(8) As String
        Dim R2(8) As String
        Dim R3(8) As String
        Dim R4(8) As String
        'randomizing loop
        For R1L As Int32 = 0 To 7 Step 1
            Dim rn As New Random
            Dim rstudent As Int32 = A1(rn.Next(0, CInt(AC1.SelectedItem)))
            'find a way not to repeat random numbers!!
            R1(R1L) = String.Format("Aluno Nº{0}{1}Turma:{2}", rstudent, Environment.NewLine, C1)
        Next

    End Sub
End Class


This post has been edited by Raminator: 08 October 2013 - 08:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Randomizing students in class

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Randomizing students in class

Posted 09 October 2013 - 10:13 AM

Make two List(Of T). One will contain all the students from one class, and the other, all the students from the other class. For each row, randomly select one of the students from the first class, place the sudent selected into the appropriate square, and remove that student from the list. Do the same for the next 7 students from the first class, placing them in the next seats in the same row.

Do the same thing with the List of students from the second class, putting them into the second row.

Repeat the above two more times, one for the third row, first class, one for the fourth row, second class.

Now you're done.

Notes:

Make your random selection within the bounds of the List (0 to whatever the last entry is). Use .RemoveAt to delete the student from the list.

As for putting it on the screen, you could use Labels, Buttons, TextBoxes, etc. It would be easiest to put them into an array.
Was This Post Helpful? 1
  • +
  • -

#3 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Randomizing students in class

Posted 09 October 2013 - 07:27 PM

Ok, so, for testing i'm only dealing with row 1 and 2, since their code is pretty much the same. I've started following your idea, and made a function to write down the student info to an image, that represents the class map. My main problem now is how to avoid the repicking of used numbers, and the argumenbt out of range exeption i get everytime on line 52([codeDim rstudent As Int32 = C1.Item(ran)[/code]).
Agument out of line exeption:

Quote

System.ArgumentOutOfRangeException was unhandled
HResult=-2146233086
Message=Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
ParamName=index
Source=mscorlib
StackTrace:
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at ProjEP.Main.Go_Click(Object sender, EventArgs e) in C:\Users\Arengorn\Documents\Visual Studio 2012\Projects\ProjEP\ProjEP\Main.vb:line 52
at System.Windows.Forms.Control.onclick(EventArgs e)
at System.Windows.Forms.Button.onclick(EventArgs e)
at System.Windows.Forms.Button.onmouseup(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
at System.Windows.Forms.Nativewindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at ProjEP.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Code:
Option Strict On
Imports System.Windows.Forms.PaintEventArgs
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms.Control
Public Class Main
    Private Sub Pan_Paint(sender As Object, e As PaintEventArgs)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Yloop As Int32 = 1 To 3 Step 1
            For Cloop As Int32 = 1 To 5 Step 1
                TC1.Items.Add(String.Format("{0}00{1}", Yloop, Cloop))
                TC2.Items.Add(String.Format("{0}00{1}", Yloop, Cloop))
            Next
        Next
        TC1.SelectedItem = "1001"
        TC2.SelectedItem = "2001"
        For Anloop As Int32 = 1 To 32 Step 1
            AC1S.Items.Add(Anloop)
            AC1E.Items.Add(Anloop)
            AC2S.Items.Add(Anloop)
            AC2E.Items.Add(Anloop)
        Next
        AC1S.SelectedItem = 1
        AC1E.SelectedItem = 16
        AC2S.SelectedItem = 1
        AC2E.SelectedItem = 16
    End Sub

    Private Sub Go_Click(sender As Object, e As EventArgs) Handles Go.Click
        'Strings with the classes names
        Dim C1 As New List(Of Int32)
        Dim C2 As New List(Of Int32)
        For Al1 As Int32 = CInt(AC1S.SelectedItem) To (CInt(AC1E.SelectedItem))
            C1.Add(Al1)
        Next
        For Al2 As Int32 = CInt(AC2S.SelectedItem) To (CInt(AC2E.SelectedItem))
            C2.Add(Al2)
        Next
        MessageBox.Show(CStr(C1.Item(15)))
        Dim R1(8) As String
        Dim R2(8) As String
        Dim R3(8) As String
        Dim R4(8) As String
        Dim usedc1 As New List(Of Integer)
        Dim usedc2 As New List(Of Integer)
        Dim line() As Int32 = {2, 51, 101, 151, 201, 251, 301, 351, 401}
        Dim rn As New Random
        For R1L As Int32 = 0 To 7
            Dim ran As Integer = rn.Next(CInt(AC1S.SelectedItem) - 1, CInt(AC1E.SelectedItem))
            If usedc1.Contains(ran) = False Then
                Dim rstudent As Int32 = C1.Item(ran)
                usedc1.Add(rstudent)
                C1.RemoveAt(rstudent)
                R1(R1L) = String.Format("Aluno Nº{0}{1}Turma:{2}", rstudent, Environment.NewLine, TC1.SelectedItem)
                MessageBox.Show(R1(R1L))
                Grid.Image = mountimg(Grid.Image, 2, line(R1L), R1(R1L), 6)
            End If
            ran = rn.Next(CInt(AC1S.SelectedItem), CInt(AC1E.SelectedItem) - 1)
        Next
        For R2L As Int32 = 0 To 7
            Dim ran As Integer = rn.Next(CInt(AC2S.SelectedItem) - 1, CInt(AC2E.SelectedItem))
            If usedc2.Contains(ran) = False Then
                Dim rstudent As Integer = C2.Item(ran)
                usedc2.Add(rstudent)
                C1.RemoveAt(rstudent)
                R2(R2L) = String.Format("Aluno Nº{0}{1}Turma:{2}", rstudent, Environment.NewLine, TC2.SelectedItem)
                MessageBox.Show(R2(R2L))
                Grid.Image = mountimg(Grid.Image, 52, line(R2L), R2(R2L), 6)
            End If
            ran = rn.Next(CInt(AC1S.SelectedItem), CInt(AC1E.SelectedItem) - 1)
        Next
    End Sub
    Function mountimg(ByVal inmage As Image, ByVal row As Int32, ByVal line As Int32, ByVal txt As String, ByVal Fsize As Integer) As Image
        Dim pimg As Image = inmage
        Dim img As System.Drawing.Graphics
        img = Graphics.FromImage(pimg)
        img.SmoothingMode = SmoothingMode.HighQuality
        img.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
        img.CompositingQuality = CompositingQuality.HighQuality
        Dim drawstr As [String] = txt
        Dim font As New Font("Times New Roman", Fsize)
        Dim brush As New SolidBrush(Color.Black)
        Dim point As New PointF(row, line)
        img.DrawString(drawstr, font, brush, point)
        Return (pimg)
        img.Dispose()
        pimg.Dispose()
    End Function
    Private Sub Augment_Click(sender As Object, e As EventArgs) Handles Augment.Click
        Dim bigimg As Bitmap = CType(My.Resources.Grid_blank, Bitmap)
        Dim fsize As New Size(bigimg.Width + 5, bigimg.Height)
        Dim picform As New Form
        picform.Size = bigimg.Size
        picform .AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink
        picform.AutoSize = True
        picform.AutoScroll = True
        Dim bpic As New PictureBox()
        bpic.Size = fsize
        picform.Controls.Add(bpic)
        bpic.Image = bigimg
        picform.Show()
    End Sub
    Public Shared Function ResizeImage(ByVal image As Image, _
      ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image
        Dim newWidth As Integer
        Dim newHeight As Integer
        If preserveAspectRatio Then
            Dim originalWidth As Integer = image.Width
            Dim originalHeight As Integer = image.Height
            Dim percentWidth As Single = CSng(size.Width) / CSng(originalWidth)
            Dim percentHeight As Single = CSng(size.Height) / CSng(originalHeight)
            Dim percent As Single = If(percentHeight < percentWidth,
                    percentHeight, percentWidth)
            newWidth = CInt(originalWidth * percent)
            newHeight = CInt(originalHeight * percent)
        Else
            newWidth = size.Width
            newHeight = size.Height
        End If
        Dim newImage As Image = New Bitmap(newWidth, newHeight)
        Using graphicsHandle As Graphics = System.Drawing.Graphics.FromImage(newImage)
            graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
            graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
        End Using
        Return newImage
        newImage.Dispose()
        image.Dispose()
    End Function
End Class


This post has been edited by andrewsw: 10 October 2013 - 03:53 PM
Reason for edit:: Removed large, previous quote.

Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Randomizing students in class

Posted 09 October 2013 - 11:49 PM

That's why I recommended a List(Of T). It acts like a list, in that you can add entries to it and remove entries from it, without having to reDim, or try to figure out how to scrunch it down when you remove something. It also act like an array, in that you can access it with an index.

Look up List(Of T) in the docs. It's MUCH better than an array for jobs like this.

dim rnd as new Random
Private StudentList1001 As New List(Of String)  ' list of students in class 1001
Private StudentList1002 As New List(Of String)  ' list of students in class 1001


You can then add students...

StudentList1001.Add( studentString ) ' add from whatever source you have them in


You can randomly choose a student...

idx = rnd.Next(0,StudentList1001.Count)  ' generate a random index between 1 and Count -1 
stdnt = StudentList1002(idx)             ' get the name at that index into a variable
StudentList1001.RewmoveAt(idx)           ' remmove the entry at the same index.


keep doing this to place 8 students in one row. Because you remove them as you assign their seats, you can't choose the same one twice.
Was This Post Helpful? 0
  • +
  • -

#5 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Randomizing students in class

Posted 10 October 2013 - 03:35 PM

Ok, i rewrote some part of the code and it's not crashing anymore, but even if using the removeat i keep getting duplicate entrys, check out these screenshots

Attached Image

Attached Image

Attached Image

Code:
Option Strict On
Imports System.Windows.Forms.PaintEventArgs
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms.Control
Public Class Main
    Dim CIMG As Image = My.Resources.Grid_blank
    Private Sub Pan_Paint(sender As Object, e As PaintEventArgs)
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Yloop As Int32 = 1 To 3 Step 1
            For Cloop As Int32 = 1 To 5 Step 1
                TC1.Items.Add(String.Format("{0}00{1}", Yloop, Cloop))
                TC2.Items.Add(String.Format("{0}00{1}", Yloop, Cloop))
            Next
        Next
        TC1.SelectedItem = "1001"
        TC2.SelectedItem = "2001"
        For Anloop As Int32 = 1 To 32 Step 1
            AC1S.Items.Add(Anloop)
            AC1E.Items.Add(Anloop)
            AC2S.Items.Add(Anloop)
            AC2E.Items.Add(Anloop)
        Next
        AC1S.SelectedItem = 1
        AC1E.SelectedItem = 16
        AC2S.SelectedItem = 1
        AC2E.SelectedItem = 16
    End Sub

    Private Sub Go_Click(sender As Object, e As EventArgs) Handles Go.Click
        'Strings with the classes names
        CIMG = My.Resources.Grid_blank
        Dim C1 As New List(Of Int32)
        Dim C2 As New List(Of Int32)
        For Al1 As Int32 = CInt(AC1S.SelectedItem) To (CInt(AC1E.SelectedItem))
            C1.Add(Al1)
        Next
        For Al2 As Int32 = CInt(AC2S.SelectedItem) To (CInt(AC2E.SelectedItem))
            C2.Add(Al2)
        Next
        Dim Psize As New Size(200, 401)
        Dim R1(8) As String
        Dim R2(8) As String
        Dim R3(8) As String
        Dim R4(8) As String
        Dim line() As Int32 = {4, 102, 202, 302, 402, 503, 603, 703}
        Dim rn As New Random
        For R1L As Int32 = 0 To 7
            Dim rstudent As Int32 = rn.Next(0, C1.Count)
            C1.RemoveAt(rstudent)
            R1(R1L) = String.Format("Aluno N{0}{1}Turma:{2}", rstudent + 1, Environment.NewLine, TC1.SelectedItem)
            CIMG = mountimg(CIMG, 2, line(R1L), R1(R1L), 12)
        Next
        For R2L As Int32 = 0 To 7
            Dim rstudent As Int32 = rn.Next(0, C2.Count)
            C2.RemoveAt(rstudent)
            R2(R2L) = String.Format("Aluno N{0}{1}Turma:{2}", rstudent + 1, Environment.NewLine, TC2.SelectedItem)
            CIMG = mountimg(CIMG, 103, line(R2L), R2(R2L), 12)
        Next
        For R3L As Int32 = 0 To 7
            Dim rstudent As Int32 = rn.Next(0, C1.Count)
            C1.RemoveAt(rstudent)
            R3(R3L) = String.Format("Aluno N{0}{1}Turma:{2}", rstudent + 1, Environment.NewLine, TC1.SelectedItem)
            CIMG = mountimg(CIMG, 203, line(R3L), R3(R3L), 12)
        Next
        For R4L As Int32 = 0 To 7
            Dim rstudent As Int32 = rn.Next(0, C2.Count)
            C2.RemoveAt(rstudent)
            R4(R4L) = String.Format("Aluno N{0}{1}Turma:{2}", rstudent + 1, Environment.NewLine, TC2.SelectedItem)
            CIMG = mountimg(CIMG, 303, line(R4L), R4(R4L), 12)
        Next
        Grid.Image = ResizeImage(CIMG, Psize, True)
    End Sub
    Function mountimg(ByVal inmage As Image, ByVal row As Int32, ByVal line As Int32, ByVal txt As String, ByVal Fsize As Int32) As Image
        Dim pimg As Image = inmage
        Dim img As System.Drawing.Graphics
        img = Graphics.FromImage(pimg)
        img.SmoothingMode = SmoothingMode.HighQuality
        img.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
        img.CompositingQuality = CompositingQuality.HighQuality
        Dim drawstr As [String] = txt
        Dim font As New Font("Times New Roman", Fsize)
        Dim brush As New SolidBrush(Color.Black)
        Dim point As New PointF(row, line)
        img.DrawString(drawstr, font, brush, point)
        Return (pimg)
        img.Dispose()
        pimg.Dispose()
    End Function
    Private Sub Augment_Click(sender As Object, e As EventArgs) Handles Augment.Click
        Dim picform As New Form
        picform.Size = New Size(CIMG.Width + 50, CIMG.Height + 50)
        '      picform.AutoScrollMargin = New Size(50, 50)
        picform.AutoScroll = True
        Dim bpic As New PictureBox()
        bpic.Size = CIMG.Size
        picform.Controls.Add(bpic)
        bpic.Image = CIMG
        picform.Show()
    End Sub
    Public Shared Function ResizeImage(ByVal image As Image, _
      ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image
        Dim newWidth As Int32
        Dim newHeight As Int32
        If preserveAspectRatio Then
            Dim originalWidth As Int32 = image.Width
            Dim originalHeight As Int32 = image.Height
            Dim percentWidth As Single = CSng(size.Width) / CSng(originalWidth)
            Dim percentHeight As Single = CSng(size.Height) / CSng(originalHeight)
            Dim percent As Single = If(percentHeight < percentWidth,
                    percentHeight, percentWidth)
            newWidth = CInt(originalWidth * percent)
            newHeight = CInt(originalHeight * percent)
        Else
            newWidth = size.Width
            newHeight = size.Height
        End If
        Dim newImage As Image = New Bitmap(newWidth, newHeight)
        Using graphicsHandle As Graphics = System.Drawing.Graphics.FromImage(newImage)
            graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
            graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
        End Using
        Return newImage
        newImage.Dispose()
        image.Dispose()
    End Function

    Private Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click
        Savediag.Filter = "PNG Image|*.png"
        Savediag.FileName = String.Format("{0}\Turma.png", System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
        Savediag.ShowDialog()
        CIMG.Save(Savediag.FileName)
    End Sub

    Private Sub Print_Click(sender As Object, e As EventArgs) Handles Print.Click
        Printdiag.ShowDialog()
    End Sub
End Class


This post has been edited by andrewsw: 10 October 2013 - 03:54 PM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3516
  • View blog
  • Posts: 12,006
  • Joined: 12-December 12

Re: Randomizing students in class

Posted 10 October 2013 - 03:55 PM

@OP There is no need to keep quoting the previous post, there is a larger Reply button further down the page.
Was This Post Helpful? 1
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Re: Randomizing students in class

Posted 10 October 2013 - 04:47 PM

Forget about the GUI, get the logic working first.

A simple throw away console application will let you experiment and tryout ideas quickly.

OK. Start by analysing the task.

Quote

Lets imagine there's a room with 32 sits, 4 rows with 8 chairs in each row.Two classes (1001 and 2001 lets say) arrive with a a total os 32 students, they should be randomized in a way where the first row only contains students from 1001, row two with students from 2001, row three with students from 1001 and row four with students from 2001, so there are no "touching" rows with students from the same class. Inside the rows the students from the class should be randomised.

I got it down to these core points
  • I'll need two classes of 16 students each.
  • Each row pick 4 random students from a particular class.
  • Alternate the class to pick from for each row.


Now I start to think about the coding.
Was This Post Helpful? 0
  • +
  • -

#8 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Randomizing students in class

Posted 10 October 2013 - 06:31 PM

AdamSpeight2008, following your tip i started a no gui protype version of it. Here's the code:
Module Module1
    Sub Main()
        Dim C1 As New List(Of Int32)
        Dim C2 As New List(Of Int32)
        Dim R1 As New List(Of String)
        Dim R2 As New List(Of String)
        Dim R3 As New List(Of String)
        Dim R4 As New List(Of String)
        Dim ran As New Random
        'the 2 classes withg 16 students each
        For L1 As Int32 = 0 To 16
            C1.Add(L1)
        Next
        For L2 As Int32 = 0 To 16
            C2.Add(L2)
        Next
        Console.WriteLine("----Row 1----")
        For R1L As Int32 = 0 To 7
            Dim rstudent As Int32 = ran.Next(0, C1.Count)
            C1.RemoveAt(rstudent)
            R1.Add(String.Format("Student N{0} - Class 1001", rstudent))
            Console.WriteLine(R1.Item(R1L))
        Next
        Console.WriteLine("")
        Console.WriteLine("----Row 2----")
        For R2L As Int32 = 0 To 7
            Dim rstudent As Int32 = ran.Next(0, C2.Count)
            C2.RemoveAt(rstudent)
            R2.Add(String.Format("Student N{0} - Class 2001", rstudent))
            Console.WriteLine(R2.Item(R2L))
        Next
        Console.WriteLine("")
        Console.WriteLine("----Row 3----")
        For R3L As Int32 = 0 To 7
            Dim rstudent As Int32 = ran.Next(0, C1.Count)
            C1.RemoveAt(rstudent)
            R3.Add(String.Format("Student N{0} - Class 1001", rstudent))
            Console.WriteLine(R3.Item(R3L))
        Next
        Console.WriteLine("")
        Console.WriteLine("----Row 4----")
        For R4L As Int32 = 0 To 7
            Dim rstudent As Int32 = ran.Next(0, C2.Count)
            C2.RemoveAt(rstudent)
            R4.Add(String.Format("Student N{0} - Class 2001", rstudent))
            Console.WriteLine(R4.Item(R4L))
        Next
        Console.WriteLine("")
    End Sub
End Module


I have the same bug of repeated outputs, an if statement can't fix it, unless i could make something like,
        If The student hasnt  been used Then
'Do stuff normally
        Else
'  Go back to the start of that loop of for, so a new random number would be picked
        End If

I have looked into the possibility of doing this but couldn't find anything concrete.
Below are the output screenshots, show the repetition.
Attached Image
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Re: Randomizing students in class

Posted 10 October 2013 - 07:55 PM

View PostRaminator, on 11 October 2013 - 02:31 AM, said:

AdamSpeight2008, following your tip i started a no gui protype version of it. Here's the code:


Quote

        'the 2 classes with 16 students each
        For L1 As Int32 = 0 To 16
            C1.Add(L1)
        Next
        For L2 As Int32 = 0 To 16
            C2.Add(L2)
        Next


These have 17 items in them count them.
9 lines of code can be refactor to 2.
Dim c1 = Enumerable.Range(1,16).ToList
Dim c2 = Enumerable.Range(1,16).ToList



Why are you repeating similar looking code?
        Console.WriteLine("----Row 1----")
        For R1L As Int32 = 0 To 7
            Dim rstudent As Int32 = ran.Next(0, C1.Count)
            C1.RemoveAt(rstudent)
            R1.Add(String.Format("Student N{0} - Class 1001", rstudent))
            Console.WriteLine(R1.Item(R1L))
        Next
        Console.WriteLine("")
        Console.WriteLine("----Row 2----")
        For R2L As Int32 = 0 To 7
            Dim rstudent As Int32 = ran.Next(0, C2.Count)
            C2.RemoveAt(rstudent)
            R2.Add(String.Format("Student N{0} - Class 2001", rstudent))
            Console.WriteLine(R2.Item(R2L))
        Next
        Console.WriteLine("")



Refactor that functionality out into a method or function.
If you think about the code as you refactor it, you may discover the reason for the cause of the repetition.
Spoiler

This post has been edited by AdamSpeight2008: 10 October 2013 - 08:05 PM

Was This Post Helpful? 1
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Re: Randomizing students in class

Posted 10 October 2013 - 08:29 PM

I think that version of that nuget package I downloaded has a tiny bug, that the version on the main desktop doesn't.

This post has been edited by AdamSpeight2008: 10 October 2013 - 08:31 PM

Was This Post Helpful? 0
  • +
  • -

#11 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Randomizing students in class

Posted 10 October 2013 - 08:31 PM

View PostAdamSpeight2008, on 11 October 2013 - 03:29 AM, said:

I think that version of that I downloaded has a bug, that the version on the main desktop doesn't.

Yeah, i'm trying to get it but it gets an error about not having a referenco for .net framework 4.0.3
Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Re: Randomizing students in class

Posted 10 October 2013 - 08:35 PM

Even so the issue of your repeating results, isn't anything to do with it.
Was This Post Helpful? 0
  • +
  • -

#13 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Randomizing students in class

Posted 10 October 2013 - 08:43 PM

Ok, I'm still really a starter at vb.net, i'm looking at the code you made and really, THANKS! There are so many things in it i didn't even know i could do! I'm having a hard time getting some parts and variables, but im getting to it.

This post has been edited by Raminator: 10 October 2013 - 08:56 PM

Was This Post Helpful? 0
  • +
  • -

#14 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Randomizing students in class

Posted 10 October 2013 - 08:56 PM

View PostRaminator, on 10 October 2013 - 07:31 PM, said:

I have the same bug of repeated outputs, an if statement can't fix it, unless i could make something like,

Well, you really should have tried the technique in my post. I can't make out what you're doing, but here's some code that implements what I suggested. Make a new project, add a button and a multiline TextBox, paste this in, and study the code and the result. I did two rows. You should be able to do the rest.

Option Strict On

Public Class Form1
    ' students. You probably want a file to hold them.
    ' read the file, split to arrays
    Private C1() As String = {"Alan", "Bob", "Charlie", "Doug", "Evan", "Frank", "George", "Harry", "Ian", "Jorge", "Kevin", "Larry", "Milton", "Nathan", "Orest", "Phil"}
    Private C2() As String = {"Abigail", "Bernice", "Cheryl", "Della", "Ellie", "Francine", "Gillian", "Harriet", "Inez", "Joanna", "Kelly", "Linda", "Millie", "Nancy", "Olivia", "Pat"}

    ' class student lists
    Private Clist1 As New List(Of String)
    Private Clist2 As New List(Of String)

    'row lists
    Private R1 As New List(Of String)
    Private R2 As New List(Of String)

    Private rnd As New Random

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim idx As Integer

        'make two class lists
        For i = 0 To 15
            Clist1 = C1.ToList
        Next

        For i = 0 To 15
            Clist2 = C2.ToList
        Next

        ' select random entries in CList, add it to row list,
        ' and remove it from the class list
        For i = 0 To 7
            idx = rnd.Next(0, Clist1.Count)
            R1.Add(Clist1(idx))
            Clist1.RemoveAt(idx)
        Next

        For i = 0 To 7
            idx = rnd.Next(0, Clist2.Count)
            R2.Add(Clist2(idx))
            Clist2.RemoveAt(idx)
        Next

        ' look at them to see there are no duplicates
        TextBox1.Text = "---- Class 1001 - ---" & vbNewLine")
        For i = 0 To R1.Count - 1
            TextBox1.Text &= R1(i) & vbNewLine
        Next

        TextBox1.Text &= vbNewLine & "---- Class 1002 ----" & vbNewLine
        For i = 0 To R2.Count - 1
            TextBox1.Text &= R2(i) & vbNewLine
        Next

    End Sub
End Class


This post has been edited by lar3ry: 10 October 2013 - 09:15 PM

Was This Post Helpful? 1
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Re: Randomizing students in class

Posted 10 October 2013 - 09:15 PM

Raminator, Stop writing code for bit and engage your brains.
Programming isn't just typing code into an IDE.
First you need analyse and think about the problem and what code you're going to eventually write.

So it Problem Solving time .

Analyse this section of your code. What is it actually doing?
For R1L = 0 To 7
   Dim rstudent  = ran.Next(0, C1.Count)
   C1.RemoveAt(rstudent)
   R1.Add(String.Format("Student N{0} - Class 1001", rstudent))
   Console.WriteLine(R1.Item(R1L))
 Next



Work your with thought it with pencil and paper.
What are the contents of each variable at each step in the process.
Was This Post Helpful? 2
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3