473,386 Members | 1,973 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,386 software developers and data experts.

store random number in array

HI I'M NEW IN VB6. i HAVE PROJECT WHICH REQUIRES ME TO STORE RANDOM NUM IN ARRAY N CAPTURE TIME FOR EACH OF THE NEW NUM IN THE ARRAY FORM. Actually my project is to calculate the average car speed. I have to code in vb for real-time application. for example i have to developed 10 array with name of the array is CarID.Each time a car passes the road the rfid reader will ditact the carID and store it in the array together the time the car reach the reader.CarID have to be generated in random num. The problem tat i'm facing is i dont know how random number generated automatically without me click any control.Plz show me the way i will do the rest.
Mar 5 '08 #1
9 4397
gobblegob
133 100+
Hi
This will generate 10 non-repeating number for you between 0-49
you can tailor it to what you need.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Dim arr(50) As Integer
  4.  
  5.  
  6. Private Sub Form_Load()
  7. Dim i As Integer
  8. For i = 0 To 49
  9.     arr(i) = i
  10. Next
  11.  
  12. Dim temp As Integer
  13. Dim swap As Integer
  14. Dim n As Integer
  15.  
  16. Randomize
  17. For n = 0 To 6
  18.     For i = 0 To 49
  19.         temp = Int(Rnd() * 50)
  20.         swap = arr(temp)
  21.         arr(temp) = arr(i)
  22.         arr(i) = swap
  23.     Next
  24. Next
  25. List1.Clear
  26. For i = 0 To 9
  27.     List1.AddItem arr(i)
  28. Next
  29.  
  30. End Sub
and instead of putting the code under Form_Load you can put it in a timer.

Gobble.
Mar 5 '08 #2
Hi
This will generate 10 non-repeating number for you between 0-49
you can tailor it to what you need.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Dim arr(50) As Integer
  4.  
  5.  
  6. Private Sub Form_Load()
  7. Dim i As Integer
  8. For i = 0 To 49
  9.     arr(i) = i
  10. Next
  11.  
  12. Dim temp As Integer
  13. Dim swap As Integer
  14. Dim n As Integer
  15.  
  16. Randomize
  17. For n = 0 To 6
  18.     For i = 0 To 49
  19.         temp = Int(Rnd() * 50)
  20.         swap = arr(temp)
  21.         arr(temp) = arr(i)
  22.         arr(i) = swap
  23.     Next
  24. Next
  25. List1.Clear
  26. For i = 0 To 9
  27.     List1.AddItem arr(i)
  28. Next
  29.  
  30. End Sub
and instead of putting the code under Form_Load you can put it in a timer.

Gobble.
hi Gobble, ur code is very helpful but i face little problem here where i try to modify ur code.

mine:
Expand|Select|Wrap|Line Numbers
  1. lblCar(n).Caption = ""
  2.  
  3.  
  4. For i = 0 To 9
  5.  
  6.     lblCar(n).Caption = arr(i)
  7.     StartTime = Now
  8.     lblStart(n).Caption = Format(StartTime, "hh:mm:ss")
  9. Next
  10.  
actualy what i'm try to do here is i want to display the random number into many label not in one label only. This code only run the random num in one label only. i have created 10 of array label to display d random num. How can i go about.plz help.
Mar 7 '08 #3
Killer42
8,435 Expert 8TB
I think the problem is that you're using variable n inside the loop, rather than the loop counter i. This rather defeats the purpose of using a loop.
Mar 7 '08 #4
hi, plz help me. Below is my code. the problem i'm facing here is when i run this code the time calculation is inaccurate. i cant figure out what is the problem when my formula is correct.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Timer1_Timer()
  2.     Randomize
  3.  
  4.     lblCar(n).Caption = Int(Rnd() * 50)
  5.  
  6.     StartTime = Now
  7.     lblStart(n).Caption = Format(StartTime, "hh:mm:ss")
  8.  
  9.     n = n + 1
  10.     If n = 4 Then
  11.         Timer1.Enabled = False
  12.     End If
  13.  
  14. End Sub
  15.  
  16. Private Sub Timer2_Timer()
  17.  
  18.     EndTime = Now
  19.     lblEnd(i).Caption = Format(EndTime, "hh:mm:ss")
  20.     Timeduration = DateDiff("s", StartTime, EndTime)
  21.     lblDuration(i).Caption = "" & Timeduration
  22.  
  23.     i = i + 1
  24.     If i = 4 Then
  25.         Timer2.Enabled = False
  26.     End If
  27.  
  28. End Sub
Mar 11 '08 #5
I think the problem is that you're using variable n inside the loop, rather than the loop counter i. This rather defeats the purpose of using a loop.

hi, plz help me. Below is my code. the problem i'm facing here is when i run this code the time calculation is inaccurate. i cant figure out what is the problem when my formula is correct.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Timer1_Timer()
  2.     Randomize
  3.  
  4.     lblCar(n).Caption = Int(Rnd() * 50)
  5.  
  6.     StartTime = Now
  7.     lblStart(n).Caption = Format(StartTime, "hh:mm:ss")
  8.  
  9.     n = n + 1
  10.     If n = 4 Then
  11.         Timer1.Enabled = False
  12.     End If
  13. End Sub
  14.  
  15. Private Sub Timer2_Timer()
  16.     EndTime = Now
  17.     lblEnd(i).Caption = Format(EndTime, "hh:mm:ss")
  18.     Timeduration = DateDiff("s", StartTime, EndTime)
  19.     lblDuration(i).Caption = "" & Timeduration
  20.  
  21.     i = i + 1
  22.     If i = 4 Then
  23.         Timer2.Enabled = False
  24.     End If
  25. End Sub
Mar 12 '08 #6
Killer42
8,435 Expert 8TB
Where are StartTime, EndTime and Timeduration declared? Um... samne goes for i and n, I suppose.

The actual code looks alright, I think. But of course I can't see what values you have, or what properties are set for the timer intervals, etc etc.

Can you give us a clearer idea of what seems to be wrong? "Inaccurate" is a pretty vague term.
Mar 12 '08 #7
Where are StartTime, EndTime and Timeduration declared? Um... samne goes for i and n, I suppose.

The actual code looks alright, I think. But of course I can't see what values you have, or what properties are set for the timer intervals, etc etc.

Can you give us a clearer idea of what seems to be wrong? "Inaccurate" is a pretty vague term.
Hi, thanks 4 d reply.sorry to msg u with unclear question. When the program runs the display 4 the time duration is wrong( wat i mean here is the output is wrong at the duration label even though the formula for time duration is correct). is it because i use two timer?. Timer1 i use 5000 interval and timer 2 i use 10000 interval. plz help me


My code:
Option Explicit
Dim StartTime As Variant
Dim EndTime As Variant
Dim n As Integer
Dim Timeduration As Variant
Dim duration As Variant
Dim i As Integer

Private Sub Timer1_Timer()
Randomize

lblCar(n).Caption = Int(Rnd() * 50)

StartTime = Now
lblStart(n).Caption = Format(StartTime, "hh:mm:ss")

n = n + 1
If n = 4 Then
Timer1.Enabled = False
End If

End Sub

Private Sub Timer2_Timer()


EndTime = Now
lblEnd(i).Caption = Format(EndTime, "hh:mm:ss")
Timeduration = DateDiff("s", StartTime, EndTime)
lblDuration(i).Caption = "" & Timeduration

i = i + 1
If i = 4 Then
Timer2.Enabled = False
End If

End Sub
Mar 12 '08 #8
Killer42
8,435 Expert 8TB
I don't think it's enough to say the display is "wrong". What does it display, and what did you expect it to display?
Mar 12 '08 #9
Killer42
8,435 Expert 8TB
Does anyone know how to total up the value of the elements in array for vb6 and count the number of elements store in array.
Since this is a new question, I'm going to split it off to its own discussion thread.
Mar 18 '08 #10

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

Similar topics

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...
16
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have...
4
by: Jack | last post by:
I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr,int n) { // const int random_number_range=1000000;
13
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
38
by: One Handed Man \( OHM - Terry Burns \) | last post by:
I have a basic question thats been niggling me, but I never found time to look at it before. If I have an enumeration such as this Fiend Enum TravelDirection North South East
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
5
by: JuAn2226 | last post by:
How do I create database which will store the random number that generated before I reset the random number and how do I check that new random number after I reset is exists in database. If exist...
0
by: JuAn2226 | last post by:
hi this my code Private Sub Form_Load() car_count = 0 Cumulative_Speed = 0 End Sub Private Sub Timer1_Timer() Dim tmpNumber As Integer
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.