Hi All, sorry for posting so many questions. Im a newbie but im trying to learn :D
With Regards to SammyB Code : - Public Class Form1
-
Private oRand As Random
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
oRand = New Random(DateTime.Now.Millisecond)
-
End Sub
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
Dim iRand As Integer
-
iRand = oRand.Next(1, 10)
-
MsgBox(iRand)
-
End Sub
-
End Class
I tried implementing it into my program but i change this line
iRand = oRand.Next(1,10) to iRand = oRand.Next(-0.3,0.3) as i want my range to be from -0.3 to 0.3. However the the value 0 kept appearing when i click the button. Why is it so? I will have to store the random values in an array as well. array(0 To 10919). Is it possible as well?
12 2507
Hi All, sorry for posting so many questions. Im a newbie but im trying to learn :D
With Regards to SammyB Code : - Public Class Form1
-
Private oRand As Random
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
oRand = New Random(DateTime.Now.Millisecond)
-
End Sub
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
Dim iRand As Integer
-
iRand = oRand.Next(1, 10)
-
MsgBox(iRand)
-
End Sub
-
End Class
I tried implementing it into my program but i change this line
iRand = oRand.Next(1,10) to iRand = oRand.Next(-0.3,0.3) as i want my range to be from -0.3 to 0.3. However the the value 0 kept appearing when i click the button. Why is it so? I will have to store the random values in an array as well. array(0 To 10919). Is it possible as well?
Try this one
for i=0 to 10919
array(i)=(rnd*0.6)-0.3
next
this generates random numbers ranging from -0.3 to 0.3
The problem is that you are storing these numbers in an Integer variable, which can only hold whole numbers. They are rounded to the nearest integer, which in this case is zero. Try using another numeric data type such as Single.
The problem is that you are storing these numbers in an Integer variable, which can only hold whole numbers. They are rounded to the nearest integer, which in this case is zero. Try using another numeric data type such as Single.
Previously i have tried changing it the double and single as well but it doesnt work also.. It gave me zero too
Try this one
for i=0 to 10919
array(i)=(rnd*0.6)-0.3
next
this generates random numbers ranging from -0.3 to 0.3
Hi i tried and it works. I just want to ask, how is it different from sammyB code? why a seed is not used in here or it is not neccessary at all? Why do you times 0.6?
Hi i tried and it works. I just want to ask, how is it different from sammyB code? why a seed is not used in here or it is not neccessary at all? Why do you times 0.6?
This code is simply generating a random number between zero and 0.6 (twice your limit) then subtracting half. The effect of this is to give you a number between -0.3 and +0.3.
Also, array() must be of a different type to the integer variable you were using before. If you didn't specify the type, then I suppose it will be the default type, which would be Variant in VB6, and I think Object in later versions.
This code is simply generating a random number between zero and 0.6 (twice your limit) then subrtacting half. The effect of this is to give you a number between -0.3 and +0.3.
Also, array() must be of a different type to the integer variable you were using before. If you didn't specify the type, then I suppose it will be the default type, which would be Variant in VB6, and I think Object in later versions.
Hmm this simple code suits my program since i only need to generate random code once throughout my entire program. thanks alot for all your help! Im on the right track now i hope! :D
Hmm this simple code suits my program since i only need to generate random code once throughout my entire program. thanks alot for all your help! Im on the right track now i hope! :D
Glad we could help. :)
Let us know how it turns out.
(If you come up with new questions along the way, please post them as new discussion threads, don't just tack them onto the end of this one. People often ignore old threads, and just look for new ones to answer, so you'll get a better response that way.)
Glad we could help. :)
Let us know how it turns out.
(If you come up with new questions along the way, please post them as new discussion threads, don't just tack them onto the end of this one. People often ignore old threads, and just look for new ones to answer, so you'll get a better response that way.)
Ok i got it! I will post my code as soon as i am done with it! :D
Hi All, sorry for posting so many questions. Im a newbie but im trying to learn :D
With Regards to SammyB Code, http://www.thescripts.com/forum/thread641768.html
I tried implementing it into my program but i change this line
iRand = oRand.Next(1,10) to iRand = oRand.Next(-0.3,0.3) as i want my range to be from -0.3 to 0.3. However the the value 0 kept appearing when i click the button. Why is it so? I will have to store the random values in an array as well. array(0 To 10919). Is it possible as well?
Glad that you figured it out. I tried to post last night, but the router crashed. Assuming that you are using VB.Net, as you discovered oRand.Next(...) always returns an integer; however, there is also .NextDouble() which is the same as VB6 rnd(). So, the code should look like: -
Public Class Form1
-
Private oRand As Random
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
oRand = New Random(DateTime.Now.Millisecond)
-
End Sub
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
Dim dRand As Double
-
dRand = oRand.NextDouble()
-
Dim dOffset As Double = -0.3
-
Dim dRange As Double = 0.3 - dOffset ' = 0.6 in this case
-
dRand = oRand.NextDouble() * dRange + dOffset
-
MessageBox.Show(dRand)
-
End Sub
-
End Class
-
Glad that you figured it out. I tried to post last night, but the router crashed. Assuming that you are using VB.Net, as you discovered oRand.Next(...) always returns an integer; however, there is also .NextDouble() which is the same as VB6 rnd(). So, the code should look like: -
Public Class Form1
-
Private oRand As Random
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
oRand = New Random(DateTime.Now.Millisecond)
-
End Sub
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
Dim dRand As Double
-
dRand = oRand.NextDouble()
-
Dim dOffset As Double = -0.3
-
Dim dRange As Double = 0.3 - dOffset ' = 0.6 in this case
-
dRand = oRand.NextDouble() * dRange + dOffset
-
MessageBox.Show(dRand)
-
End Sub
-
End Class
-
Thanks! I tried and it works too. May i know what is the difference between this code and the code from ChillUmesh?
Thanks! I tried and it works too. May i know what is the difference between this code and the code from ChillUmesh?
Hi. I am trying to generate random numbers between 1000 and 9999. - Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
Randomize()
-
Dim iRand As Integer
-
iRand = oRand.Next(1000, 9999)
-
-
' This is where I want 4 DIFFRENT RANDOM NUMBERS BUT THEY'RE RANDOMLY THE SAME AHHH''''
-
'TextBox1.Text = (iRand)
-
'TextBox2.Text = (iRand)
-
'TextBox3.Text = (iRand)
-
'TextBox4.Text = (iRand)
-
End Sub
How can I get 4 different random numbers at each click? Thanks in advance for the help.
Look at it logically. You generated a random number, and placed it in a variable. However many times you access that variable it will still contain the same value, unless you change it.
I'd say the most obvious options are... - Assign another random number to variable iRand before each of the statements where you use it.
- Create an array with four elements, and place a random number in each, then use those. (This is probably overkill, though.)
- Don't bother placing the number in a variable at all - just use the oRand.Next(1000, 9999) in each case.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: Joe |
last post by:
Hi, I have been working on some code that requires a high use of random
numbers within. Mostly I either have to either:
1) flip a coin i.e. 0 or 1, or
2) generate a double between 0 and 1.
I...
|
by: Peteroid |
last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers.
But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0
with resolution of a double (i.e., every possible...
|
by: fieldfallow |
last post by:
Hello all,
Is there a function in the standard C library which returns a prime
number which is also pseudo-random?
Assuming there isn't, as it appears from the docs that I have, is there
a...
|
by: Jim Michaels |
last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or
mysql.
I have not been able to do either. I get the same number back several times
from PHP's mt_rand() and from mysql's...
|
by: porterboy76 |
last post by:
If you only use a 32 bit seed for a random number generator,
does that mean you can only ever produce a maximum of
2^32 (approx 4 billion) different sequences?
What about the Mersenne Twister,...
|
by: Daniel |
last post by:
Hey guys
Using Random(), how random is it, is it possible to be predicted? I have a
requirement for a very good random number generator. I was doing something
such as:
Random randomSeed = new...
|
by: chico_yallin |
last post by:
I just wana make a random id number based on4 digits-for examples??
Thanks in Advance
Ch.Yallin
|
by: badcrusher10 |
last post by:
Hello.
I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work?
THIS IS WHAT I NEED TO DO:
Professor Snoop wants a program...
|
by: bilgekhan |
last post by:
What is the correct method for generating 2 independent random numbers?
They will be compared whether they are equal.
What about this method:
srand(time(0));
int r1 = rand();
srand(rand());...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
| |