473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Empty values return by random funciton

132 100+
Hi guys,

I'm completely stupified by this silly randomizer function.

It's often returning me empty values and I have no idea why.

I want to generate random number between 0 and 5 but I want to prevent that the same number is being generated twice in a row.

Here's the code
Expand|Select|Wrap|Line Numbers
  1. Dim LastNumber as Integer
  2.  
  3. Private Sub Form_Load()
  4.     Randomize
  5.     LastNumber = 99
  6.     Tmr_Timer.enabled = true    
  7. End Sub
  8.  
  9. Private Sub Tmr_Timer()
  10.     MsgBox (Random)
  11. End Sub
  12.  
  13. Public Function Random()
  14.     Dim RNumber As Integer
  15.  
  16.     RNumber = Int(Rnd(1) * 5) + 0
  17.  
  18.  
  19.     If RNumber <> LastNumber And RNumber <> vbNull Then
  20.  
  21.         LastNumber = RNumber
  22.         Random = RNumber
  23.     End If
  24.  
  25. End Function
  26.  
Anybody has an idea why this doesn't work?

Thanks,
Kenneth
Jan 6 '12 #1

✓ answered by Rabbit

Random = Random()

9 1419
Rabbit
12,516 Expert Mod 8TB
It's because when the number repeats, you never generate another number. Also, you're not going to get the range 0-5, you'll only get 0-4.
Jan 6 '12 #2
Cainnech
132 100+
Thanks Rabbit for your response, but what would be the solution then? Because I can't just call Random again...
Jan 6 '12 #3
Rabbit
12,516 Expert Mod 8TB
Yes you can. That's exactly the solution. Call random until you get a non-repeating number.
Jan 7 '12 #4
Cainnech
132 100+
Well I think there's another problem then because I added the Random to the code but I still get empty values.

Expand|Select|Wrap|Line Numbers
  1. Public Function Random()
  2.     Dim RNumber As Integer
  3.  
  4.     RNumber = Int(Rnd(1) * 6) + 0
  5.  
  6.     If RNumber <> LastNumber And RNumber <> vbNull Then
  7.  
  8.         LastNumber = RNumber
  9.         Random = RNumber
  10.  
  11.     Else
  12.  
  13.         Random
  14.  
  15.     End If
  16.  
  17. End Function
  18.  
Jan 7 '12 #5
Rabbit
12,516 Expert Mod 8TB
That's because the first random call is still the function that will get returned to the timer and you haven't assigned it the value of the next random call.
Jan 7 '12 #6
Cainnech
132 100+
And how can I assign the value of the next random call then?
Jan 7 '12 #7
Rabbit
12,516 Expert Mod 8TB
Random = Random()
Jan 7 '12 #8
Cainnech
132 100+
Yes that did it.
It's a bit confusing since in VB6 you normally call a function just by using the Functionname without the brackets.

So when I was calling Random in the else statement, I thought I was actually calling the function again.

But now it's making a bit more sense.

Thanks Rabbit!
Jan 7 '12 #9
Killer42
8,435 Expert 8TB
If you have a look in the Insights section for VB 4/5/6, there's an article on generating non-repeating random numbers. It includes a self-contained function to generate them for you.

Also, take care when writing recursive code, it can easily get out of control. (That is, code which calls itself like your Random function here). It's easy to get stuck in a loop and fill up the stack.
Jan 7 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
0
by: David | last post by:
On every web browser except Safari, this website works great. (Well, by "every" I mean Mozilla, Netscape, and Internet Explorer, for Mac and Windows). The site is: http://www.ruleofthirds.com ...
10
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object...
10
by: Marshall Belew | last post by:
I'm trying to synchronize a network app that uses random numbers generated by System.Random. Rather than pass every randomly generated number, I just pass the seed. I'm seeing a result that leads...
5
by: Ryan Ternier | last post by:
I'm having an issue with an SQL insert statement. It's a very simple statement, and it's causing too much fuss. strSQL = "INSERT INTO tblFieldLayouts(TypeID, FieldID, OrderID, Hidden) VALUES("...
3
by: laurenq uantrell | last post by:
I'm trying to return an integer from the following table that returns the number of unique cities: tblEmployees Name City John Boston Frank New York Jim Omaha Betty ...
12
by: NuB | last post by:
The validation controls are giving me a headache. here is what i'm trying to do and so far what I've tried has not worked. I need to hide my grid if the page is not valid how can i accomplish...
15
by: Steven Macintyre | last post by:
Hi all, I need to retrieve an integer from within a range ... this works ... below is my out puts ... it just does not seem so random ... Is there perhaps a suggestion out there to create a...
6
by: Mike Langworthy | last post by:
i can not seem to get this code to work. someone please help using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.