how can i make a random number not to always appear every time i open the program
my code always prints or present same number every time i open and terminate and open it again and again it always shows same number but if i click a button for random it does show random number
this code just my example:
note: this is just an example not my true code in my program...

Dim rndm As Integer
    Randomize
    rndm = Int(4 * Rnd())
    Select Case rndm
    Case 1
        list1.additem = 1
    Case 2
        list1.additem= 2
    Case 3
        list1.additem = 3
    Case 4
        list1.additem = 4
    End Select

Dani AI

Generated

Two separate issues are visible in the original post that explain the “same number every start” behaviour.

First, the seed/usage of VB’s RNG. In classic VB/VBA the Rnd sequence is pseudo‑random and is seeded by Randomize. If the seed you use is the same at each program launch (or the timer resolution is coarse), the first value can repeat on every start. Calling Randomize once at program startup is the normal fix; repeatedly reseeding (or creating a fresh RNG object on every call) can produce identical results. ’s idea of persisting a value to the registry is unnecessary unless you specifically need reproducible results across runs.

Second, the range logic needs checking. Multiplying Rnd() by 4 and taking the integer can produce 0..3, so your Select Case 1..4 will not map correctly. That off‑by‑one is a common source of “weird” outputs even when randomness itself is fine.

Practical advice:

  • If you’re on VB.NET, prefer a single shared Random instance and reuse it rather than repeatedly creating new Randoms:

    ' Module-level
    Private ReadOnly RNG As New Random()
    Dim value As Integer = RNG.Next(1, 5)  ' returns 1..4

    See the official documentation for System.Random for details: System.Random documentation.

  • If using VB6/VBA, call Randomize once at startup (don’t reseed right before every Rnd call) and adjust the arithmetic so the produced integer covers 1..4. See Microsoft’s notes on the Randomize statement and Rnd function: and Rnd function.

Also consider whether you actually need persistent randomness (saved seed): only use persistence when you want the same sequence reproducibly; otherwise fix seeding and range logic and reuse a single RNG. Finally, as hinted, populate your list directly rather than using an overly large Select Case.

Recommended Answers

All 2 Replies

why you need a case for populating the list.

Why not use a simple FOR loop for the purpose.

Your problem is that the random number is not saved to your system, hence the same number every time it starts. Save the random number to your registry and let the app select a random number from this number. Google "Save number to registry in VB6", plenty of samples to choose from.

You can also save this number to a database, call it from there and then call for a random number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.