473,386 Members | 1,828 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.

Random Numbers Generator

Hi All, sorry for posting so many questions. Im a newbie but im trying to learn :D
With Regards to SammyB Code :

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private oRand As Random
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         oRand = New Random(DateTime.Now.Millisecond)
  5.     End Sub
  6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  7.         Dim iRand As Integer
  8.         iRand = oRand.Next(1, 10)
  9.         MsgBox(iRand)
  10.     End Sub
  11. 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?
May 28 '07 #1
12 2507
Hi All, sorry for posting so many questions. Im a newbie but im trying to learn :D
With Regards to SammyB Code :

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private oRand As Random
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         oRand = New Random(DateTime.Now.Millisecond)
  5.     End Sub
  6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  7.         Dim iRand As Integer
  8.         iRand = oRand.Next(1, 10)
  9.         MsgBox(iRand)
  10.     End Sub
  11. 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
May 28 '07 #2
Killer42
8,435 Expert 8TB
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.
May 28 '07 #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.
Previously i have tried changing it the double and single as well but it doesnt work also.. It gave me zero too
May 28 '07 #4
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?
May 28 '07 #5
Killer42
8,435 Expert 8TB
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.
May 28 '07 #6
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
May 29 '07 #7
Killer42
8,435 Expert 8TB
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.)
May 29 '07 #8
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
May 29 '07 #9
SammyB
807 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private oRand As Random
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         oRand = New Random(DateTime.Now.Millisecond)
  5.     End Sub
  6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  7.         Dim dRand As Double
  8.         dRand = oRand.NextDouble()
  9.         Dim dOffset As Double = -0.3
  10.         Dim dRange As Double = 0.3 - dOffset    ' = 0.6 in this case
  11.         dRand = oRand.NextDouble() * dRange + dOffset
  12.         MessageBox.Show(dRand)
  13.     End Sub
  14. End Class
  15.  
May 29 '07 #10
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:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private oRand As Random
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         oRand = New Random(DateTime.Now.Millisecond)
  5.     End Sub
  6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  7.         Dim dRand As Double
  8.         dRand = oRand.NextDouble()
  9.         Dim dOffset As Double = -0.3
  10.         Dim dRange As Double = 0.3 - dOffset    ' = 0.6 in this case
  11.         dRand = oRand.NextDouble() * dRange + dOffset
  12.         MessageBox.Show(dRand)
  13.     End Sub
  14. End Class
  15.  
Thanks! I tried and it works too. May i know what is the difference between this code and the code from ChillUmesh?
May 30 '07 #11
bIGMOS
5
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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. Randomize()
  3. Dim iRand As Integer
  4. iRand = oRand.Next(1000, 9999)
  5.  
  6. ' This is where I want 4 DIFFRENT RANDOM NUMBERS BUT THEY'RE RANDOMLY THE SAME AHHH''''
  7. 'TextBox1.Text = (iRand)
  8. 'TextBox2.Text = (iRand)
  9. 'TextBox3.Text = (iRand)
  10. 'TextBox4.Text = (iRand)
  11. End Sub
How can I get 4 different random numbers at each click? Thanks in advance for the help.
Dec 19 '07 #12
Killer42
8,435 Expert 8TB
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.
Dec 19 '07 #13

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...
3
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...
5
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...
104
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...
12
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...
13
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,...
3
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...
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
6
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...
26
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());...
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: 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...
0
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...
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
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
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.