Forum Home
Press F1
 
Thread ID: 29629 2003-01-27 02:31:00 Visual Basic -Random numbers Julius Caesar (3079) Press F1
Post ID Timestamp Content User
116746 2003-01-27 02:31:00 Hi,

I'm a real newbie with Visual Basic. And I'm finding it really hard to understand at times.

What i want to know, using (random numbers), is how can I like stop a a particular number or even a series of numbers from repeating twice?

Eg. say my results from each textbox for the following numbers were:

box 1= 1, box2= 2, box3= 3, boxt4= 4, box5= 5, box6= 2

as you can see that the No.2 appeared twice in the series...

So, then what I code to instruct visual basic from doing this each time.

Can someone please give me of a formula example

I hope to hear from you.
Thanks
Julius Caesar (3079)
116747 2003-01-27 02:34:00 sorry.....

What code can I use to instruct Visual basic from doing this again each time?

Can someone please give me of a formula example?

I hope to hear from you.
Thanks
Julius Caesar (3079)
116748 2003-01-27 03:11:00 Well, random numbers generated by computers are not random . What you get is a "pseudo random sequence" . One attribute is that if you run a programme a number of time, you will get the same sequence of numbers . That's not a fault; it's usually useful (for debugging a programme), and there is always a way to avoid it: some languages use a Random function called as "random(blah)" . If you don't give "blah" a value, or give it a constant (like "0") it will always give the same sequence . If you give it a number generated by the seconds of the time, or some otjer "random" starting number (called the "seed") you will get a different sequence . Most modern languages have a "Randomize" command . Just call it once, before using random the first time . That will make the random function give different sequences .

But repeated numbers in a random sequence are going to happen . They sequences wouldn't be random (or even pseudo random), if there weren't repeats . You will have to test for repeats and remove them in your own code .

But you stillwon't be able to predict winning Lotto numbers from any programme . :D
Graham L (2)
116749 2003-01-27 03:50:00 I know what you're trying to do just unsure how one would go about it.

you're after a permutation as opposed to a combination.
-=JM=- (16)
116750 2003-01-27 04:22:00 I rang my son, who is a Maths teacher, and he said (he knows nothing of Visual Basic) that if you are storing in an array (you probably are, but if not ,you probably should be !) then you should be able to have an embedded loop. then the first number will not do a check, however the second number will check the last number, the third number will check the first and second number etc etc.

I hope that is of some help !
:)
Misty (368)
116751 2003-01-27 20:33:00 Hail Caesar! Refer to Code Toad (www.codetoad.com) on the function to create random numbers & paste this code in a module. On the click event of the command button on your form assign code to call the RandomNumbers function like this .


Private Sub Command1_Click()

'Creates 6 unique numbers in each of the 6 text boxes

Dim a

a = RandomNumbers(6, 1, 6, True)

Text1 = a(0)

Text2 = a(1)

Text3 = a(2)

Text4 = a(3)

Text5 = a(4)

Text6 = a(5)


End Sub


The number after the variable "a" is the element within the array starting from zero. For example, a(3) is the fourth number in the array. Like you Im familiar with VBA not VB, but Ive tried this in a VB project and it worked fine for me
parry (27)
116752 2003-01-27 21:25:00 Hi Julius,
If you want non-repeating random numbers, you need to keep a list of the previous numbers. For each additional random number, check the list to see if it has occurred before, if it has, then ignore it and generate another, if it hasn’t it’s OK and you can include it on the list.
You seem to use only positive integers in your random sequence, but don’t say what range these fall within (eg. 1 to 10, 0 to 1 million, or whatever).
Try the following VB code, that generates 10 non-repeating random positive integers in the range 1 to 20. You can easily change the range and how many random values you want.
************************************************** *******************
Sub ran()
Dim r(100) As Integer, j As Integer, s As Integer, a As Integer
Randomize
‘rem next line says there are 10 numbers on your list
For j = 1 To 10
‘rem next line generates random integer in range 1 to 20
eminem: a = Int(Rnd() * 20) + 1
‘rem next 3 lines check if new number is already in list
For s = 0 To j - 1
If a = r(s) Then GoTo eminem
Next s
‘rem next line includes non-repeated new random number on your list
r(j) = a
‘rem below displays your non-repeating random list in MS Excel
‘rem alternatively put them in text boxes as you like
Cells(j + 1, 1) = j: Cells(j + 1, 2) = r(j)
Next j
End Sub
************************************************** *******************

Also I must remark on Graham L’s post above. Most of this is good. However, he isn’t right in saying that “Repeated numbers in a random sequence are going to happen. The sequences wouldn’t be random if there weren’t repeats.” This is only OK if you confine yourself to potentially repeating random sequences. A simple example of a non-repeating random sequence is to make a list of the integers 1 to 10 entries and then randomly re-order their positions on the list.
Repeating random sequences and non-repeating random sequences just need slightly different treatments. What you are doing is fine, no logical problems. (If in any doubt on any of this check out Michel Loeve’s Probability Theory 3d Edn, New York, Van Rostrand Reinhold Company, 1963, or similar standard reference).
You might re-post letting me know if the above code works for you and if it is the sort of thing you had in mind.
rugila (214)
116753 2003-01-28 02:06:00 Hi everyone,

Thanks heaps for your help. "Massive"

To Rugila and Parry, I will let you know, how I got on. But, don't expect a quick result yet. I'm still on the learning curve, and yet I have to take a bit more time in working this out....sorry bout that!

Thanks for your help!
Julius Caesar (3079)
116754 2003-01-28 02:49:00 Rugila: if you exclude repetitions you haven't got a "random" sequence.

By definition, randomness means that you can't predict the next one (and that includes predicting that it won't have occurred before).

Reordering a list "randomly" does not produce randomness. It produces (in your example, 10x9x...x1) predictable lists, the permutations. Our questioner wants, I guess, 6 numbers from some limited range (1 .. 40? :D), that is the combinations. Again it's not truly random (there are only about 13 million combinations of 6 out of 40). And the Random function does not produce true random sequences. (It's very diffiuclt to generate good pseudorandoms).

It's possible to get carried away with what "standard references" say ;-). You still can't compute the winning numbers for Lotto. :-(
Graham L (2)
116755 2003-01-28 04:04:00 Hi Graham L.
Thanks for your views. They're most interesting and I really appreciate that.
However, I hope you accept a couple more brief comments from me.
(a) A standard example for explaining randomness is tossing a fair coin. Outcome = heads or tails. Nobody can predict which of these the next fair toss will produce, so the outcome is random by your definition. But you seem also able to somehow predict that neither head nor tail had happened before( "and that includes predicting that it won't have occurred before"). So what did happen on previous tosses?
(b) re Standard references. Loeve's probability text, also defining randomness, has been used in some of the world's top universities. I could quote some alternative sources (not obtained through Google) that I think could shed more light on your definition of randomness, which with due respect I don't think would find very wide acceptance.
(c) Maybe you don't think that much of "standard references". Certainly I totally agree with you that anyone should use whatever definition of randomness they want. But if explain to someone without much background in randomness and associated topics (actually I spent a few years doing this sort of thing for a job) standard references do have their uses.
(d) What exactly is the background for your randomness definition? Did you invent it or did you use a standard reference or did you get it some other way. That's supposed to be a serious, and helpful, question. :)
rugila (214)
1