• 0

Random Number Generator


Question

24 answers to this question

Recommended Posts

  • 0

It'd help to know what language you need it in. Either I'm blind or you added that VB note as I posted this.

In C# I'd do:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> usedNumbers = new List<int>();
            string outv = "";
            Random r = new Random();

            for (int i = 0; i < 100; i++)
            {
                calc:
                int nextVal = r.Next(0, 101);
                if (usedNumbers.Contains(nextVal)) goto calc;
                outv += nextVal.ToString() + " ";
                usedNumbers.Add(nextVal);
            }
            outv = outv.Substring(0, outv.Length - 1);
            Console.Write(outv);
        }
    }
}

Here it is converted to VB:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

Namespace Test
	Class Program
		Private Shared Sub Main(args As String())
			Dim usedNumbers As New List(Of Integer)()
			Dim outv As String = ""
			Dim r As New Random()

			For i As Integer = 0 To 99
				calc:
				Dim nextVal As Integer = r.[Next](0, 101)
				If usedNumbers.Contains(nextVal) Then
					GoTo calc
				End If
				outv += nextVal.ToString() & " "
				usedNumbers.Add(nextVal)
			Next
			outv = outv.Substring(0, outv.Length - 1)
			Console.Write(outv)
		End Sub
	End Class
End Namespace

  • 0
  On 03/10/2010 at 00:50, unr3al said:

I need to make a random number generator where it displays all the numbers 1-100 in a text box. All numbers need to be displayed once in a random order.

This has to be coded in visual basic. Can anyone help me out with the code?

Much appreciated if you do help!

It couldn't be easier :D Lucky for you VB already has a sort of RNG so really all you have to do is work on the presentation...

I found this doing a google on 'visual basic rng':

Private Function RandomNumber() as Integer

Randomize Timer

RandomNumber = Int(Rnd * 100)

End Sub

  • 0
  On 03/10/2010 at 01:00, omnicoder said:

It'd help to know what language you need it in. Either I'm blind or you added that VB note as I posted this.

In C# I'd do:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> usedNumbers = new List<int>();
            string outv = "";
            Random r = new Random();

            for (int i = 0; i < 100; i++)
            {
                calc:
                int nextVal = r.Next(0, 101);
                if (usedNumbers.Contains(nextVal)) goto calc;
                outv += nextVal.ToString() + " ";
                usedNumbers.Add(nextVal);
            }
            outv = outv.Substring(0, outv.Length - 1);
            Console.Write(outv);
        }
    }
}

Here it is converted to VB:

I'm so sorry :(

It has to be coded in VB. My fault, I updated my post.

I'd love if you could convert that to VB6.

  • 0

Private Shared Sub Main(args As String())
                        Dim usedNumbers As New List(Of Integer)()
                        Dim outv As String = ""

                        For i As Integer = 0 To 99
                                calc:
                                Dim nextVal As Integer = Rand(0, 101)
                                If usedNumbers.Contains(nextVal) Then
                                        GoTo calc
                                End If
                                outv += nextVal.ToString() & " "
                                usedNumbers.Add(nextVal)
                        Next
                        MsgBox(outv)
                End Sub

Can't test if that compiles as I no longer have VB6 but should be pretty close.

  • 0
  On 03/10/2010 at 01:06, omnicoder said:

Private Shared Sub Main(args As String())
                        Dim usedNumbers As New List(Of Integer)()
                        Dim outv As String = ""

                        For i As Integer = 0 To 99
                                calc:
                                Dim nextVal As Integer = Rand(0, 101)
                                If usedNumbers.Contains(nextVal) Then
                                        GoTo calc
                                End If
                                outv += nextVal.ToString() & " "
                                usedNumbers.Add(nextVal)
                        Next
                        MsgBox(outv)
                End Sub

Can't test if that compiles as I no longer have VB6 but should be pretty close.

I can't get it to compile. I'm a VB6 beginner, so I might be doing something wrong.

I appreciate your help though.

  • 0

Try

Private Sub Generate(args As String)
                        Dim usedNumbers As New List(Of Integer)()
                        Dim outv As String = ""

                        For i As Integer = 0 To 99
                                calc:
                                Dim nextVal As Integer = Rand(0, 101)
                                If usedNumbers.Contains(nextVal) Then
                                        GoTo calc
                                End If
                                outv = outv + nextVal.ToString() & " "
                                usedNumbers.Add(nextVal)
                        Next
                        MsgBox(outv)
                End Sub

  • 0
  On 03/10/2010 at 01:17, omnicoder said:

Try

Private Sub Generate(args As String)
                        Dim usedNumbers As New List(Of Integer)()
                        Dim outv As String = ""

                        For i As Integer = 0 To 99
                                calc:
                                Dim nextVal As Integer = Rand(0, 101)
                                If usedNumbers.Contains(nextVal) Then
                                        GoTo calc
                                End If
                                outv = outv + nextVal.ToString() & " "
                                usedNumbers.Add(nextVal)
                        Next
                        MsgBox(outv)
                End Sub

20896312.jpg

  • 0
  On 03/10/2010 at 01:22, omnicoder said:

Ah VB6 doesn't have a List class. I'll have to change it to an array. Gimmie a minute.

From the original post, and admittedly my recent programming skills are virtually non-existent as I learned to code pre-Windows, the above code won't do exactly what is requested.

As I understand the task,

"I need to make a random number generator where it displays all the numbers 1-100 in a text box. All numbers need to be displayed once in a random order."

Generating 100 random numbers between one and 100 won't guarantee there won't be repeats.

Instead, and I can't write the code for it, simply put the numbers 1-100 in an array, list, or whatever... Then you simply perform say, 100 shuffles of 1 number for another in the list, each shuffle randomly determined, then spit the results out. The exact number of shuffles is arbitary but you'd want enough to theoretically at least shuffle each number once. So shuffling each number in the list at least once would ensure about as random as you're gonna get.

  • 0
  On 03/10/2010 at 01:38, krasch said:

From the original post, and admittedly my recent programming skills are virtually non-existent as I learned to code pre-Windows, the above code won't do exactly what is requested.

As I understand the task,

"I need to make a random number generator where it displays all the numbers 1-100 in a text box. All numbers need to be displayed once in a random order."

Generating 100 random numbers between one and 100 won't guarantee there won't be repeats.

Instead, and I can't write the code for it, simply put the numbers 1-100 in an array, list, or whatever... Then you simply perform say, 100 shuffles of 1 number for another in the list, each shuffle randomly determined, then spit the results out. The exact number of shuffles is arbitary but you'd want enough to theoretically at least shuffle each number once. So shuffling each number in the list at least once would ensure about as random as you're gonna get.

omnicoder's code ensures each number will only be displayed once. It uses a calculation to determine a random number between 1 and 100. It stores displayed numbers in a list (an array, in the case of VB6) and checks the list before showing the next number, each time; if the number has been displayed, it will search for another number between 1 and 100.

His C# code works very nicely.

  • 0
  On 03/10/2010 at 01:41, Calum said:

omnicoder's code ensures each number will only be displayed once. It uses a calculation to determine a random number between 1 and 100. It stores displayed numbers in a list (an array, in the case of VB6) and checks the list before showing the next number, each time; if the number has been displayed, it will search for another number between 1 and 100.

His C# code works very nicely.

I need omnicoder's code to work in VB6!

Thanks for the replies guys.

  • 0
  On 03/10/2010 at 01:45, unr3al said:

I need omnicoder's code to work in VB6!

Thanks for the replies guys.

Out of curiosity, how much experience have you had with both programming and VB6? I'm just wondering, again out of curiosity, do you understand exactly what omnicoder's code does? If not, it may be beneficial if it is explained to you, so you learn from this :)

  • 0
  On 03/10/2010 at 01:50, Calum said:

Out of curiosity, how much experience have you had with both programming and VB6? I'm just wondering, again out of curiosity, do you understand exactly what omnicoder's code does? If not, it may be beneficial if it is explained to you, so you learn from this :)

I just started a couple of weeks ago.

I don't have any experience in C#; Visual Basic 6 is currently the only language I have experience with.

Explanations would help!

  • 0
  On 03/10/2010 at 01:53, unr3al said:

I just started a couple of weeks ago.

I don't have any experience in C#; Visual Basic 6 is currently the only language I have experience with.

Explanations would help!

I see. Once you and omnicoder have sorted out code which works and functions as it should, make sure you understand every line - every single part of it - and if not, ask questions here until you do :)

It really is important you understand what every line of this code does and why certain decisions were made. When I was first starting out learning programming, I was guilty of just copying code of those that helped me without ensuring I understood it; that only made it harder for me in the longrun (although I'm decent at it now and ensure I learn as much as possible).

  • 0
  On 03/10/2010 at 01:58, Calum said:

I see. Once you and omnicoder have sorted out code which works and functions as it should, make sure you understand every line - every single part of it - and if not, ask questions here until you do :)

It really is important you understand what every line of this code does and why certain decisions were made. When I was first starting out learning programming, I was guilty of just copying code of those that helped me without ensuring I understood it; that only made it harder for me in the longrun (although I'm decent at it now and ensure I learn as much as possible).

Thanks for the advice.

  • 0

This is a VBS script that will generate a radom list of 100 numbers.

It uses the Randomize method and Dictionary object, you should be

able to convert it to your VB 6 project.

Run this script using Cscript.exe or you will get a lot of messageboxes.

Save As RandomNumbers.vbs

Dim Dic :Set Dic = CreateObject("Scripting.Dictionary")
Dim a,i
 Randomize
    For i = 0 To 99
     a = Int((100 - i + 1) * Rnd + 1)
     If Not Dic.Exists(a) Then
      Dic.Add  a, a 
      WScript.Echo a
      WScript.Sleep 100
     End If
    Next

  • 0
  On 03/10/2010 at 16:54, jake1eye said:

This is a VBS script that will generate a radom list of 100 numbers.

It uses the Randomize method and Dictionary object, you should be

able to convert it to your VB 6 project.

Run this script using Cscript.exe or you will get a lot of messageboxes.

Save As RandomNumbers.vbs

Dim Dic :Set Dic = CreateObject("Scripting.Dictionary")
Dim a,i
 Randomize
    For i = 0 To 99
     a = Int((100 - i + 1) * Rnd + 1)
     If Not Dic.Exists(a) Then
      Dic.Add  a, a 
      WScript.Echo a
      WScript.Sleep 100
     End If
    Next

Could you please convert this to VB6?

  • 0

It would be a lot more efficient to generate an array of numbers from 1 to 100, and then shuffle it for a while. It'd result in O(n) complexity rather than O(n?), like the suggested solution.

Generating an array of 100 numbers from 1 to 100 is trivial.

As for shuffling, generate two random numbers between 0 and 99 (or 1 to 100 depending on whether VB6 indexes from 0 or 1, I'm not sure), and then swap the values located at these indexes in the array. Repeat at least 50 times. The more you repeat the more random it gets.

  • 0

i'm not trying to be an asshat here.. but these type of threads should be closed.

this is not helping you learn anything, and the type of problem you are solving is clearly a typical school/uni assignment..

i would have hoped people to have more sense than to just post up answer for you huh.gif

in any case, there is PLENTY enough detail in this thread to set you on your way to be able to do it in VB6.. you don't even have to work out how to solve the issue, but just write it on in another language..

good luck :)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • From what I understand, RAID 1 makes a copy of one disk. How does it do this? If something happened to one drive, could I take the other one out, plug it into a PC and use it like a normal disk? I have two hard drives that have the same information on - a bit of protection if one drive fails. But I've just been manually copying the data twice.
    • Microsoft SharePoint gets Modern Page Templates to speed up page creation by Paul Hill SharePoint, Microsoft’s enterprise content management solution that allows organizations to set up internal pages and share documents, has just got a big update with the new Modern Page Templates. Microsoft said that it’s going to be rolling out the feature to customers globally between early July and early August. Organization members that want to set up pages on SharePoint using the new templates can do so from multiple entry points including the Site Home (the home of a Communication or Teams site), App Bar (the persistent navigation pane on the left of SharePoint), and the New Web Part (a webpart that displays and creates news posts directly on pages). By giving colleagues multiple access points to the new templates, there’s more chance they’ll be found and used. By including plenty of swanky templates, Microsoft is making it so you can spend less time designing pages, and more time filling them with content that really matters. If you want to create a new page using the templates, just go to New > Page and select a template “From Microsoft”. If you were making a news post, you would go to New > News post then you’d be presented with the new Template Gallery where you can select a custom template created by your colleagues under “Saved on this site”. While these new templates are coming to customers globally next month, Microsoft is already busy working on new features to make templates easier to discover and make them more useful. For example, it is building tenant-wide custom templates that can be published across sites for organization-wide use, and it’s working on Copilot templates that you can use when creating pages with Copilot. Neither of these features was given a release timeline. Customers won’t need to do anything to start using Modern Page Templates as they’ll be available automatically as it rolls out worldwide across tenants. For those who want to dive deeper, Microsoft will be publishing additional documentation and guidance about this feature soon.
    • Times are changing: https://arstechnica.com/gaming...ndows-11-ars-testing-finds/
    • Unless there is “bug” that all of a sudden sends your messages to Meta. Where have I heard this before?!
    • Maybe, just maybe... and it isn't you... there are some people who like the Windows 11 UI (for whatever reason) and want a better backend.
  • Recent Achievements

    • One Month Later
      Leonard grant earned a badge
      One Month Later
    • Week One Done
      pcdoctorsnet earned a badge
      Week One Done
    • Rising Star
      Phillip0web went up a rank
      Rising Star
    • One Month Later
      Epaminombas earned a badge
      One Month Later
    • One Year In
      Bert Fershner earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      539
    2. 2
      ATLien_0
      205
    3. 3
      +FloatingFatMan
      168
    4. 4
      Michael Scrip
      150
    5. 5
      Som
      126
  • Tell a friend

    Love Neowin? Tell a friend!