• 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

    • Microsoft still tries really hard to convince people about Windows 11. It does not realize, the harder it tries the more people hate it. And it is not people's fault. It is MS that tries violently to own your computing environment without your consent. Cheers MS!
    • I've got a basic black and white laser printer that's connected via USB and doesn't do wifi etc. I think I'm going to be just fine.
    • Edge 138 is out with AI-powered history search and other changes by Taras Buria Microsoft has released Edge 138, the latest major update for the browser. Version 138.0.3351.55 introduces some interesting changes and new features, such as AI-powered history search. There are also several bug fixes and security patches. For regular users, the biggest and most important change in Edge 138 is AI-powered history search. This feature allows you to find sites in your history using synonyms, phrases, or misspelled words. Microsoft uses an on-device model, which does not send your data anywhere. Note that this feature is rolling out gradually, which means it might take a few days or weeks to show up on your system. Another useful change is new performance notifications. Performance and Extensions Detector notifications may appear in the main menu when the browser detects performance dips to help users learn about available performance-optimization tools. Autofill settings received a new consent toggle, which allows Microsoft to improve the autofill capabilities by collecting field names as you browse. This only applies to field names, such as "First Name, "Email," etc. It does not send the data you enter or autofill to Microsoft. Other changes include the following: Use the Primary work profile as the default profile to open external links. With this feature, for Windows, Edge checks if the Primary Work Profile exists and makes it the default profile for opening external links if available. Microsoft 365 Copilot Chat Summarization in Microsoft Edge Context Menu. This feature helps users quickly unpack and ask questions about their open page. Copilot on the Microsoft Edge New Tab Page (NTP). Users may see suggested work and productivity-related Copilot prompts in their search box on the NTP page. Adding support for viewing Sensitivity labels applied to a Microsoft Information Protection (MIP) Protected PDF. Enterprise customers can view sensitivity labels applied to MIP protected PDF to be well informed of the data classification to enable them to handle such sensitive documents. And here is what was fixed: Fixed an issue that caused WebDriver automation to fail in Microsoft Edge versions 133 and later. Fixed an issue where re-enabled textarea elements remained non-editable. This issue affected activating a role assignment in Privileged Identity Management. Finally, Edge 138 patches six security vulnerabilities, three of which were Microsoft Edge-specific, and the remaining three originated from Chromium. You can find details about those fixes here. The next Microsoft Edge update, version 139, is expected in the week of August 7, 2025.
    • “Never trust any statistics that you didn't forge yourself.”
    • Per the linked article: "Based on testing performed by Microsoft in December 2024 using Geekbench 6 Multi-core score comparing a selection of Windows 10 PCs with Intel Core 6th, 8th, and 10th generation processors and Windows 11 PCs with Intel Core 12th and 13th generation processors." I get that this is just advertising and all, but damn, I can smell the BS all the way over here. How about benchmarking 10 vs 11 on that same 13th gen processor? Apples and oranges make a lovely fruit salad but a terrible comparison. I mean shoot, my Windows 10 PC running a Ryzen 7 is faster than a Core2Duo running Windows 7, so Windows 10 is clearly faster. 🙄
  • Recent Achievements

    • Week One Done
      suprememobiles earned a badge
      Week One Done
    • Week One Done
      Marites earned a badge
      Week One Done
    • One Year In
      runge100 earned a badge
      One Year In
    • One Month Later
      runge100 earned a badge
      One Month Later
    • One Month Later
      jfam earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      562
    2. 2
      +FloatingFatMan
      177
    3. 3
      ATLien_0
      167
    4. 4
      Michael Scrip
      125
    5. 5
      Xenon
      121
  • Tell a friend

    Love Neowin? Tell a friend!