Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    Visual Basic




True Random Number Generator


I've created this code to create a random number that is truely random. I've tested it out and it appears to work. Usually if you don't you will notice a pattern on the display. Anyone have any things that could make this better or any experience doing this and have some improvements?

Public Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
'this function gets the color number under the mouse pointer.

'Load a form. Place a label in one of the corners with a fixed bordertype.
'paste this code in and hit F5!

Public rngready

Private Sub Form_Load()
MsgBox "Starting Number Generator, keep moving the mouse on the window."
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y _ As Single)
For tmp = 1 To 1000 '(If your system is slow, lower this)
Randomize Timer + X + Y
'Uses the mouse pointer as part of the seed to fill the form with colors.
X = Int(Rnd * Form1.Width)
Y = Int(Rnd * Form1.Height)
r = Int(Rnd * 255)
g = Int(Rnd * 255)
b = Int(Rnd * 255)
Form1.PSet (X, Y), RGB(r, g, b)
Next tmp

Label1.Caption = GetPixel(Form1.hdc, Form1.ScaleX(X, Form1.ScaleMode, 3), _ Form1.ScaleY(Y, Form1.ScaleMode, 3))
Label1.Refresh
End Sub

Private Sub Timer1_Timer()
rngready = 1
'Here you could use a public variable and turn it on after a
'specific interval to ensure that the form is filled with random colors.
'When the variable is on, then start storing numbers!
End Sub
"None of us is as smart as all of us"




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Random Number Generator
how can u set a random value to your variables?

Random Number Generator
Hi,

I need to create a random number but I have no idea where to start. All I want to do is to create my random number and place it in a variable called RandomNumber. Can anyone help please!!!

Help With Random Number Generator
I'm am creating a program that produces 6 random lottery numbers the code i got so far is but what do i have to do to prevent 2 of the same number being generated twice.

Code:
Option Explicit
Dim RandomNumber(1 To 6)
Dim Amount As Integer
Private Sub Command1_Click()
Timer1.Enabled = True
Command1.Enabled = False
End Sub
Private Sub Command2_Click()
List1.Clear
Command1.Enabled = True
Amount = 0
End Sub
Private Sub Timer1_Timer()
Dim Index As Integer
For Index = 1 To 6
Randomize
RandomNumber(Index) = Int(48 * Rnd) + 1
RandomNumber(Index) = Format(RandomNumber(Index), "0#")
List1.AddItem RandomNumber(Index)
Amount = Amount + 1
If Amount = 6 Then
Timer1.Enabled = False
End If
Index = Index + 1
Next Index
End Sub
Thank You

Random Number Generator
How do I generate a series of random numbers that don't repeat?

I have:

Code:
Dim Winners(14) as integer
Dim Hopefuls as integer

Randomize

picWinners.Cls
Hopefuls = InputBox("Enter the number of hopefuls","")

For x = 1 to 14
Winners(x) = Int((Hopefuls * Rnd) + 1)
picWinners.Print Winners(x)
Next x


This gives me the right number of random numbers but they might repeat...Edit: fixed code tags

Random Number Generator
Hello,

I'm trying to create a program that randomly generates a number from 1 to 10, displays the number in a text box, and removes that number from the list, so that the next click of the button will generate a number from the numbers left and so on untill all ten randomly generated numbers are all in different text boxes. I just need help with the removing from the list problem. Should I use an array for this? If so how? Any help would be appreciated.

Thanks

Random Number Generator
I need to know how to generate random numbers in either a For..Next, or Do...Loop structure. Completly Stummped. Thanks

Help With Random Number Generator
I've created a program that randomly generates 6 numbers between 1 and 49, but my main problem now is how do i prevent the program randomly generating two or more of the same number

Random Number Generator!!
I need a random number generator. One that can generate a number from zero (0) to eight (8).

Can anyone please help me?

I Need A Random Number Generator
Hi Guys,

I need a Random Number generator that will select a number withing a given range. For example, 1 person will be allocated a ball number, which I will store in another table.

Then the generator should generate a number, but the number must be within the range of the ones in the ball number table.

Please help

Random Number Generator
hi,

Can anyone tell me how to generate random numbers in vb 6.0

Thanks,
abhiram

Random Number Generator
Hello,

I am trying to make a loop that will distribute pictures from one form to another in random spots of an array of 4x4 picture boxes:

but it does not work what is wrong?

VB Code:
Public Function Rnd(ByVal Low As Long, ByVal High As Long) As Long  Rnd = Int((High - Low + 1) * Rnd) + LowEnd Function Private Sub Form_Load()     For i = 0 To 15        random = Rnd(0, 15)        imgPicture(random).Picture = frmFlags.preimage(random).Picture    Next i End Sub

It will only put the first picture into the first picture box of the array.

Oh and there are only 8 flags that I'm putting into an array of 16 boxes so each flag must be put in twice...

Random Number Generator
In VB6, how can I monitor mouse movement and keybroad input latency in the background to a table of 1024 entries for random number generation? Is there any VB6 program source for cryptographic pseudo random number generator (PRNG)?

Random Number Generator
I want to use random numbers between 1 and 25 in a VB6 program. I've noticed that the sequence generated by

Int(Rnd * 25)

is always the same. Anyone have a good random number generator code you would be willing to share? Thanks!

--Ideally, one that would sequence numbers 1 through 25 in random order.

Random Number Generator Help
I'm using this to generate random 6 digit numbers in an access database. The only thing is, it generates duplicates. Is there any way I can modify it or recode it to not generate duplicates?


Private Sub Ticket___Enter()
Dim FinalNum As Long

MinNum = "000000"
MaxNum = "999999"
FinalNum = MinNum + Fix(Rnd * (MaxNum - MinNum + 1))
Me.Ticket__ = FinalNum
End Sub

Random Number Generator
How would create a random number generator between the limits of 1 and 20 for a multiple choice test??

Random Number Generator
How would create a random number generator between the limits of 1 and 20 for a multiple choice test??

Random Number Generator
How would create a random number generator between the limits of 1 and 20 for a multiple choice test??

Random Number Generator
How would create a random number generator between the limits of 1 and 20 for a multiple choice test??

Random Number Generator
I am making a program that utilizes a random number generator. The one I am using is from the VB Help files:

T = Int((5 * Rnd) + 1)

I seem to recall that this particular format is not all that random and there is a better way to do it. Does anyone know what that is or have any suggestions?

Thanks!

Jeff

Random Number Generator
I am making a program that utilizes a random number generator. The one I am using is from the VB Help files:

T = Int((5 * Rnd) + 1)

I seem to recall that this particular format is not all that random and there is a better way to do it. Does anyone know what that is or have any suggestions?

Thanks!

Jeff

Does VB Have A Random Number Generator?...
... and if so, how do I access it?

(ps. did you know that 'VB' is a beer in Australia? )

Random Number Generator Help
Okay here is the situation.  I am creating this database for the city hall I work in.  I want to create this scheduling program to match team 1 and team 2 up. The teams can play at time1 time2 or time3 at field1 field2 field3 or field4. That is alot of randomizing going on and quite frankly I see smoke from my reflection in the computer monitor.  To add to the dilemma I will have to query which teams play on which nights and in which leages.  I think that this may be my starting point so that much is done. On average there are 8 - 12 teams per league per night.  And on top of that I need to be able to edit the times because some teams have special requests.  All this will come from a click event on a button.

Help please

Random Number Generator
Hi all

here is my code for a random number generator
it picks a random 3 digit number using 1-9 how can
I can I make it so it uses 0-9 instead if I use this code:

J = Int(0-9 * Rnd) + 1

I get negative values

any ideas

thanks

lneilson




Private Sub Command8_Click()

Cls
Dim I As Integer
Dim J As Integer
Randomize
For I = 1 To 3
J = Int(9 * Rnd) + 1
Print J
Next I
End Sub

I Need A Random Number Generator
Hey all, I am trying to create a program that will allow my kids to input any 2 associated items (such as a state name and the name of the capital) into a database. When the program is run I need to generate the first item being displayed (such as the name of the State) by generating a random number between 1 and the number of items in the table. Then I need to load a list box with the correct answer along with several wrong answers (the names of the capitals). I think I can code all of this except that I cannot figure out how to generate the initial random number to choose the first item displayed.

If anyone out there can explain how Rnd function works or give me an example of how to code this I would really appreciate it. I have looked through the MSDN library but it doesn't make too much sense to me and I need to do this as quickly as I can so that my kids can start using it before the school year gets too far along. I have also searched on VBCity.com but only found code that would generate a random number between 0 and 1.

Again if anyone can help me I would sure appreciate it. Have a great day.


Cliff R.
Mesa, AZ
Check out my website at http://crogers.org
Being an organ donor is giving the greatest gift that someone can give.
It is giving the gift of life. Please register to be an organ donor.

Problem With Random Number Generator
no matter what the random number generated will be the same if i reset the program (first few numbers are like 8750251, 6815248, and 8545521, but after i restart and regen a few numbers i get the same 3 set)

I use int(rnd() * 123456789) to create random numbers


i want it to log the numbers into a txt file, and tell it that if that number = a set of numbers in the text file, regen a number.... but i dunno how to do that, im guessing here



Code:
open "numbers.txt" for input as #1
Newnumber:
text1.text = int(rnd) * 123456789)
if text1.text = numbers.txt then goto newnumber '(i have problems with goto commands some help would be appericated)
close #1
Open "numbers.txt" for append as #2
print #1, text1.text '(the random number that has been generated)
close #2

now thats just a guess and i know that wont work, but i need help with the txt file calling and now to get it to look at it, i looked at the I/O tutorial but cant figure it out still

Anyone Have A Good Random Number Generator?
Preferable one that does not use VB6's Randomize or Rnd functions.
I have a lot of functions that fire many many many times that call Rnd -1 and my whole app can no longer make random numbers because of this..

Random Number Generator Coding
In my database I want to give players ratings but based 3 levels. Good, Mediocre or Poor. If I select Good then it should give ratings between 7 and 9, mediocre 5 and 7 and poor 3 and 5.

eg

Name Level Passing Free Kicks Tackling Shooting

Beckham Good 9 9 7 8

Advanced Random Number Generator
Hello all,

I stumbled upon your site as I was searching for some info about how to alter the native VB random number generator...I am writing some code to generate many random subsets of data from a much larger dataset (i.e. monte carlo simulation). For example, I want to create 10,000 10 element segements from a 1000 element dataset. Each 10 element segment should have unique components from the larger 1000 element dataset.

My stragedy is to generate a random permutation (from 1 to 1000) and use the first 10 numbers to access 10 elements in the array. For example, suppose the random permutation generates 10,900,800, 45, 600, etc..the first number (10) would go to the cell 10 in the 1000 element array and put that number into a segement. This process is repeated for the remaining 9 numbers. Each new segement requires me to generate a new permutation.

If I use the conventional randomize and and rnd statements, is there is the possibility that I could generate identical random numbers in building my 10 element segments that would lead to duplicate elements in a segment? My additional fear is that the more segements I build, I might see a repeating pattern of random numbers.

In Gary Cornell's book "Visual Basic 6 from the ground up", he hints at a way to modify the built-in random number generator but I cannot find out what he is talking about. He claims it is in ch.10 but I cannot find it... Does anybody know what I am talking about?


Any thoughts?

Thanks,

Radman

Random Number Generator Problem
I am doing a program where I build a two-dimensional array and then populate it with elements by using a random number generator. It is supposed to have random numbers between 1 and 100. I keep getting a compiler error saying that I have the wrong number of dimensions. Is this something about the way I set up the array or is it the code for the random number generator? This is what I have so far:

Dim x(1 To 5, 1 To 10) As Single
Dim n As Long, i As Long
For i = 1 To 5
x(i) = Int(Rnd * 100) + 1
Next i
For i = 1 To 5
Print i, x(i)
Next i
For n = 1 To 10
x(n) = Int(Rnd * 100) + 1
Next n
For n = 1 To 10
Print n, x(n)
Next n

Random Number Generator.. With Sead?
Well as i was going through and testing out a random number generator i had made, i remembered something that a guy was talking about. Where the computer does not randomly generate numbers, well sure it does it while the program is running, but unless you actually use the computers seconds thingy.. (sead) it will come up with the same thing over and over each time you start the program. Well i was wondering if there was anyway to make it so you program has a sead, or at least make it so when you start the program and hit generate, it comes up with 5 then you can close the program then bring it up again, then hit generate and it comes up with 1.

It is suppose to be a dice generator.. for Role Play

Please help,
Thanks

Random Number Generator Problems
i am trying to creat a program the randomly chooses 20 numbers out of 80, but every time i click the random button it only generates 18, 19, 20, or 21 numbers. could anyone tell me if the code i am using is the right one. the code is as follows:

Dim r(20) as integer
Dim a as integer

function random(low as long, up as long)
randomize
random = int(rnd(20) * up + 1) + low
end function.

then when i try to access that i use this:

For a = 0 to 19
r(a) = random(1, 80)
next

for a = 0 to 19
(other code follows here)
next
end sub

it could be generating 20 numbers but 1 or 2 of them could be the same number, is there an easy way to generate 20 different numbers from 1 to 80?

Tracy

Suggestions On Random Number Generator
Anybody have a suggestion on how to use a random number generator in VB 6. I need it to be able to provide a number between a range I give it which is right now from 1 to about 6000.

Different Multi-digit Random Number Generator
i need smth that would generate a multi-digit
random number, differently each time i call the function
for ex,
23432423423-first call
234231111-second call
and when i exit hte program, and load again
874859435-first call
1231230202020-second call

complete randomization, no order,

is this possible?

Random Number Generator Using Intel Hardware
Hai...anyone know how to using/access Intel RNG (Intel 810 chipset) to generate random number?

Whats The Random Number Generator Api Command?..i Want To Use This Instead Of Rnd*Num
i want to use the api command for generatoring random numbers... someone posted it ages ago and i cant find it...

Random Number Generator Stopped Working
Hi, I'm writing a simple game in VB6 which selects one of three balls at random to drop towards a paddle underneath it. The user then presses a button to activate the correct paddle and catch the ball. This worked fine for a while and the balls dropped randomly but then it stopped working. This is the code I was using for the random number generator:


Code:
Private Function RandomNumber() As Integer
'generate a random number between 0 and 2
Randomize
randNum = Int(Rnd * 3)
'End Function


This is the only place the random number is used:

Code:
Dim RandNum As Integer
If dropping = False Then 'if no ball is dropping
RandomNumber 'generate a random number
dropping_ball = randNum
dropping = True 'drop a ball
End If


I tried outputing RandNum in a textbox and saw that it is always at zero. I also tried giving it a different value when it was initialised but it always reset to zero. This makes me think that the generator is working but it could just be picking the same number each time. I have no idea why it's doing this if so because this code is exactly the same as before when it was working. As it was working, I had no need to change it so I didn't.

Does anyone have any idea how to fix this?

Thanks,
Mooncinder

Random Number Generator To Get An Item From Access
hi Everyone,

I am trying to write a programme for a membership draw. I have created a membership database with 360 members.

What I want to do is get VB to pick a random number from 1 to 360, then go to the database and then display what the field is located at that number.

I have created the database so that there is a "number Record" as the key.

ie if VB selects 60, I want VB to show what information is at record number 60.

I have looked through the forums for an answer, but cant quite find what I am looking for.

Hope someone can help

How Do I Seed The Random Number Generator?[answered]
What function do I use to seed the random number generator in VB? Also, I assume it just takes a single argument which is a long integer; is this correct?

True Random Number
hi i have used other peoples code from these forum but i am still finding no joy in finding code that gives me a random number evey other code seems to be extremly biased towards a certain number ( 7 in my case ) why is this and what is the best way to create a totally random number
Thanks Fraser

True Random Number
I have been using this random command from the Tutor's Corner:


Code:
Int(Rnd() *10 )

This may create a random number but however if you use the code


Code:
MsgBox Int(Rnd() *10 )

No matter how many times I stop and start the app I always get the same numbers in the same order. I was wondering if I can generate TRUE random numbers

Thanks

Lucas

Random Number If = True
Guys,

This question may sound complicated... And I was hoping it's possible to do it. what I wanted is to have random number to change when variable is true or 1, then change the random number can change back to original when variable is false or 0 for example here's snippet:


VB Code:
Dim Lucky As ByteDim LuckyNum As Variant If Lucky = 1 Then  Lucky = 258 + 2 + 4 - 1 Else   Lucky = 258 - 2 - 4 + 1 End If LuckyNum = LuckyPrint format(LuckyNum, "000" ' If true Print will show 263 otherwise it will go back to number 258  




I have tried this similar code using command1 button as if I pressed to be true, and I end up with overflow and could not understand why, Also, I was expecting number to increase sequently in delay time for instance when its true or 1, the display will show 258,260,264 then 263. When its false or 0 it will countdown sequently 264,260,258 and etc. Perhaps, I have done wrong or anything. done search on this site seeing if anyone ever encountered or trying these methods... and end up with no luck. Can anyone help?

Random Number Fuction If (variable)= True
Guys,

This question may sound complicated... And I was hoping it's possible to do it. what I wanted is to have random number to change when variable is true or 1, then change the random number can change back to original when variable is false or 0 for example here's snippet:


Code:
Dim Lucky as Byte
Dim LuckyNum as Variant

If Lucky = 1 Then

Lucky = 258 + 2 + 4 - 1

else

Lucky = 258 - 2 - 4 + 1

End If

LuckyNum = Lucky
Print format(LuckyNum, "000" ' If true Print will show 263 otherwise it will go back to number 258

I have tried this similar code using command1 button as if I pressed to be true, and I end up with overflow and could not understand why, Also, I was expecting number to increase sequently in delay time for instance when its true or 1, the display will show 258,260,264 then 263. When its false or 0 it will countdown sequently 264,260,258 and etc. Perhaps, I have done wrong or anything. done search on this site seeing if anyone ever encountered or trying these methods... and end up with no luck. Can anyone help?

Help With Random Map Generator
I am working on a random map generator for my game but i have a problem. The maps all come out so that all coasts run diagnaly. This is becuase the map is created by an errosian like process and tiles touched by more water errode faster. My question is how can I fix this while still having tiles touched by more water errode faster? The code is attached but beware it is un-commented.

Random Map Generator
anyone knows how to randomly generate map in 2D land?

Random Gun Generator
Hey guys & gals .. I'm new to the site so bare with me a bit I tried searching around for this but wasn't able to come up with anything that seemed to fit exactly what I needed.

I play America's Army and what I'm trying to do is this:

Make a small program in VB to randomly switch people's weapons in America's Army. I want there to be a settings menu so users of the program can select which gones they'd want to use by putting a check box next to them.

On the main window of the program will be a button to users can CLICK that button when they want to start the random gun script, and then click it again to stop. When they 1st click it and turn it on, I'd like this program to generate a text file called rand.txt to a certain directory EVERY 10 seconds. This text file will be opened by ME, or whoever uses it, in America's Army by typing exec rand.txt BUT, I want to be able to have this program do something like this:

Each line will have something to the effect of:
admin forceclass 1 ak
admin forceclass 2 sf
etc..
etc..

Here's the thing though, in that settings box that the users will open to select which guns they'd like, I want this program to be able to randomly go through and assign the guns to each of the admin forceclass lines ..... so ..... after 10 seconds, when the script is generated AGAIN, we might have:

admin forceclass 1 g
admin forceclass 2 ak
etc..
etc..

There will be about 30 lines going 1 - 30 with admin forceclass ALWAYS at the front and the names of the weapons randomly picked out and assigned to these numbers.


So .... the jist of it is: After settings are set, users press button to "start script". This initially generates rand.txt and then every 10 seconds after that, the file is generated again, with randomly assigned gun values EACH time. This way, when I'm in the game, I can play one round of America's Army after having run rand.txt and then the next round, run it again and get competely seperate guns.


I REALLY hope someone can help me with this. I've been wanting to get working on this but I haven't used VB in a while and have been getting back into it so I'm trying to figure out how to set it all up.

Any help would be GREATLY appreciated .... I've become quite the VB noob b/c I haven't used it in so long lol ...

Thanks to anyone that can help. I've been reading through my old VB books trying to find some stuff to get this working.

Random Name Generator
not really too sure what part of the forums to post this in... im looking to make a random name generator, so ive been scouring the web looking for any huge databases of firstnames and surnames that i can download (preferably in excel or txt format)... anyone no where i can find one??

Random Name Generator Help
I was trying to make a Random Name Generator.

Using txtMin, txtMax, txtAmount.

txtMin = Minimum Number of Characters
txtMax = Maximum Number Of Characters

So it Makes a Name Ranging from Min(10) To Max(15) Characters.

Than txtAmount is How many Names it Makes.

Than Writes all the names it made to Names.txt

I assume you would need to use Array's, and i've never used them Before. so... If you could give me some code with some Comments.

I started on this code, but after fiddling around with arrays.. and failed. I got exasperated, and i posted here.

Any Help would be apreciated very much.

Random # Generator
i got this code from a friend who had it said it worked for her, but i am not as fortunate. pleaze, tell me why, if you can . . .

Dim Numbers(0 To 60) As Integer

Private Sub cmdsort_Click()
Dim intCOUNT As Integer
Dim rndNUMBER As Integer
For intCOUNT = 0 To 60
rndNUMBER = ((Rnd * 300) + 100)
Numbers(intCOUNT) = rndNUMBER
Next intCOUNT

thanx

The Random Generator
Hereís the problem, I have a textbox which Iím able to enter one letter into. The textbox is called txt1. When I enter a letter, another (random) letter should appear in a label called lblRand1.

I have an idea of how I would like the random feature to work but Iím unsure how to implement it. Hereís how I would like it to work: I want the random generator to take the letter in the textbox ASCII value and come up with a number that should either be added or taken away from that ASCII figure. The trouble is that the new figure should still be a letter. Lets say that I input the letter T into the textbox; the random feature should generate a number between Ė19 and 6. This is because T = 84 in ASCII so, 84 + 6 is 90 which is the ASCII value of Z; -19 because 84 Ė 19 = 65 which is the ASCII value of A. So as you can see, whatever the randomly generated result would be, the ASCII code would always result in a letter. As another example just to make sure that you fully understand, if I was to enter the letter A into the textbox, the result from the random generator should be between 0 and 25. This is because A is the lowest letter in the ASCII scale so taking anything away would produce something other than a letter, hence the 0. The 25 because 25 added to 60 (A) would produce 90 (Z).

I really hope you understand what Iím trying to say. If somebody could produce some code, which can accomplish this task, I would really be very grateful. Thanks

Copyright © 2005-08 www.BigResource.com, All rights reserved