11 Replies - 2131 Views - Last Post: 21 June 2015 - 10:26 PM Rate Topic: ***-- 1 Votes

#1 TheCoolFactor   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 07-February 15

Generating random numbers less than zero ?

Posted 18 June 2015 - 01:29 PM

i was searching for a generator and i found it this is the code:

Dim rn As New Random
ExampleBar.Value = rn.Next(1, 10)


it randomly generates a number from 1 to 10. but it wont generate anything less than 0 what i tried is:

Dim rn As New Random
ExampleBar.Value = rn.Next(-999, 10)


and all i get is out of range error and crashed when i clicked debug

i thought it was bigger than 32bit so i tried :
Dim rn As New Random
ExampleBar.Value = rn.Next(-50, 10)


same thing happend when i clicked debug

(i didnt know what how much digits 32bit contained when writing the code above when writing this topic, so i googled it and the result was:

-2,147,483,648 through 2,147,483,647 (unsigned: 0 through 4,294,967,295)

so i tried:

Dim rn As New Random
ExampleBar.Value = rn.Next(-1, -10)


and still the same result when i clicked debug.

what i want to know is how to generate a value lesser than 0

sources:
https://en.wikipedia.org/wiki/32-bit

Is This A Good Question/Topic? 0
  • +

Replies To: Generating random numbers less than zero ?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15427
  • View blog
  • Posts: 61,803
  • Joined: 12-June 08

Re: Generating random numbers less than zero ?

Posted 18 June 2015 - 01:36 PM

Right off the bat - did you read the documentation?

https://msdn.microso...=vs.110%29.aspx
https://msdn.microso...=vs.110%29.aspx


You'll notice the '.Next' should not have an issue with a negative value. If your crash is happening on:

ExampleBar.Value = rn.Next(-999, 10)

... and the "rn.Next(-999, 10)" doesn't seem to be the problem, what part of that line is the problem? Probably "ExampleBar.Value ". I have zero clue what 'examplebar' is or what 'examplebar.value' is.. but you probably have an idea and a place to look into.
Was This Post Helpful? 0
  • +
  • -

#3 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,640
  • Joined: 07-September 13

Re: Generating random numbers less than zero ?

Posted 18 June 2015 - 01:39 PM

I am unsure why it wont let you insert negative value however what does it matter it produces a value? just make the value negative?

instead of saying -999 to 0 go 0 to 999 and whatever value comes out convert it to a negative.

Like so:

      Dim rnd As New Random()

        Dim nextValue As Integer = -rnd.Next(0, 100)

        Me.Text = nextValue.ToString

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,311
  • Joined: 12-December 12

Re: Generating random numbers less than zero ?

Posted 18 June 2015 - 01:49 PM

If it is a ProgressBar then you get that error, but it also tells you why the error occurs:

Quote

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll
Additional information: Value of '-10' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.

This post has been edited by andrewsw: 18 June 2015 - 01:50 PM

Was This Post Helpful? 0
  • +
  • -

#5 TheCoolFactor   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 07-February 15

Re: Generating random numbers less than zero ?

Posted 18 June 2015 - 02:48 PM

View Postmaceysoftware, on 18 June 2015 - 01:39 PM, said:

I am unsure why it wont let you insert negative value however what does it matter it produces a value? just make the value negative?

instead of saying -999 to 0 go 0 to 999 and whatever value comes out convert it to a negative.

Like so:

      Dim rnd As New Random()

        Dim nextValue As Integer = -rnd.Next(0, 100)

        Me.Text = nextValue.ToString


this one worked but it wont show the value in a label if lesser than 0

code:

Public Class Form1
    Dim rn As Random
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim rn As New Random
        Dim nextValue As Integer = -rn.Next(0, 100)

        Label1.Text = ProgressBar1.Value
    End Sub

 
End Class



wont show the value on the label if lesser than 0

what im trying to say is that i want it to function just like:

Public Class Form1
    Dim rn As Random
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ProgressBar1.Increment(-1)
    End Sub

 
End Class



so i want it to remove(subtract) random values from the progress bar
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1538
  • View blog
  • Posts: 3,869
  • Joined: 01-February 13

Re: Generating random numbers less than zero ?

Posted 18 June 2015 - 03:24 PM

Hi,

I just wanted to add some info about a few things. The first is that if you read the msdn documents on the Random.Next Method that you are using you will see that the Return Value says the following:

Type: System.Int32
A 32-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned.


So, the first code you showed would only choose a random number from 1 to 9, not 1 to 10. In short, the returned random values will only range from the minValue to the (maxValue - 1).

Also, as has been mentioned, the Random.Next method can be used with negative numbers with no problem so, that is not the problem.

We also know that the returned value of the Random.Next method is an Int32 type which you have shown the Min and Max values for. So, we know that -999 through 10 is within the Int32 Min and Max values and that, that is not the problem ether.

That only leaves one thing that could be causing the error, that would be the ExampleBar.Value. As was mentioned, the error that is shown on that line probably gives you more info about the problem. What type of control is the ExampleBar? What Type is the Value property of the ExampleBar, an Integer type, Decimal type, or other type?

Does the ExampleBar have Minimum and Maximum Properties that you can set? If so then make sure they are set to your minimum value of -999 and your maximum value of 10.
Was This Post Helpful? 0
  • +
  • -

#7 TheCoolFactor   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 07-February 15

Re: Generating random numbers less than zero ?

Posted 18 June 2015 - 04:10 PM

its a Progressbar:

preview

This post has been edited by andrewsw: 22 June 2015 - 03:21 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#8 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5509
  • View blog
  • Posts: 14,497
  • Joined: 18-April 07

Re: Generating random numbers less than zero ?

Posted 18 June 2015 - 04:20 PM

Btw... I don't think the progressbar value can start with a negative value. The minimum is zero if I remember correctly. So if you are generating negative values, you are still going to have to put it on a scale which is positive.

https://msdn.microso...(v=vs.110).aspx

States in Exceptions...

ArgumentException - The value specified for the property is less than 0.

:)
Was This Post Helpful? 0
  • +
  • -

#9 TheCoolFactor   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 07-February 15

Re: Generating random numbers less than zero ?

Posted 18 June 2015 - 05:19 PM

View PostMartyr2, on 18 June 2015 - 04:20 PM, said:

Btw... I don't think the progressbar value can start with a negative value. The minimum is zero if I remember correctly. So if you are generating negative values, you are still going to have to put it on a scale which is positive.

https://msdn.microso...(v=vs.110).aspx

States in Exceptions...

ArgumentException - The value specified for the property is less than 0.

:)/>/>

i dont want it to start with a negative value, when the form loads it will give the progressbar a random value and the button will keep taking random amounts from it

Public Class Form1
    Dim rn As Random
   

 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'progressbar1 will get a random value when the form loads'


        'Default' Progress bar' 
        ProgressBar1.Maximum = 100

        'first time setting values'
        ProgressBar1.Value = rn.Next(1, 10)

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'what im trying to do is to take a random value(or amount) from the progressbar1 since it already have a value'
        'i tried all the above and it didnt give me the expected result'
        'i want it to function just like the code:'
        ProgressBar1.Increment(-1)

        'but i want it to take a RANDOM values from the progressbar1 since it does have a value'
    End Sub
End Class



also im really sorry if i didnt correctly specify what i wanted i will describe all the 'things' next time.
Was This Post Helpful? 0
  • +
  • -

#10 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1538
  • View blog
  • Posts: 3,869
  • Joined: 01-February 13

Re: Generating random numbers less than zero ?

Posted 18 June 2015 - 05:28 PM

If it is a ProgressBar that you are using then that is the problem as has pointed out already. A ProgressBar`s Minimum value can only go as low as 0 (zero) and you are trying to set the Value to a negative number below 0.

Do you really need the ProgressBar to indicate numbers below 0?

Perhaps if you explain more about what this ProgressBar is used for and why you need its Value to indicate negative numbers it would help us suggest the best solutions.

My first thought about what i know so far is that you could set the ProgressBar Minimum property to 0 and its Maximum property to the result of subtracting your Minimum negative number from your Maximum number.

For example, if i wanted a Minimum of -100 and a Maximum of 50 i would set the ProgressBar`s Maximum property to 50 minus -100 which is 150. At this point the ProgressBar`s range is from 0 to 150 so, to get the range that we require (-100 to 50) from the ProgressBar`s Value property we need to subtract 100 from the Value. That would produce the range we want (-100 to 50). Here is an example that uses these values.

Place 1 Button, 1 ProgressBar, and 1 Label on a Form for this example.
Public Class Form1
    Private R As New Random

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = 150 '150 is the result from (50 minus -100) or Max - Min

        ProgressBar1.Value = 100 'this would be the 0 center point of our range

        'Displays -100 to 50 in the Label to indicate the ProgressBar`s Value
        Label1.Text = (ProgressBar1.Value - 100).ToString
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'increment the ProgressBar value by a random number between -10 and 10
        ProgressBar1.Increment(R.Next(-10, 11))

        'Displays -100 to 50 in the Label to indicate the ProgressBar`s Value
        Label1.Text = (ProgressBar1.Value - 100).ToString
    End Sub

End Class


Was This Post Helpful? 1
  • +
  • -

#11 TheCoolFactor   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 07-February 15

Re: Generating random numbers less than zero ?

Posted 18 June 2015 - 05:53 PM

i changed the code a 'little' bit so this is the final result:

Public Class Form1
    Private R As New Random

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = 100

        ProgressBar1.Value = 50

        Label1.Text = (ProgressBar1.Value).ToString
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ProgressBar1.Increment(R.Next(-10, 11))

        Label1.Text = (ProgressBar1.Value)
    End Sub

End Class





thank you all for helping me

This post has been edited by andrewsw: 22 June 2015 - 03:21 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#12 BobRodes   User is offline

  • Product Manager
  • member icon

Reputation: 604
  • View blog
  • Posts: 3,085
  • Joined: 19-May 09

Re: Generating random numbers less than zero ?

Posted 21 June 2015 - 10:26 PM

Quote

i dont want it to start with a negative value, when the form loads it will give the progressbar a random value and the button will keep taking random amounts from it

The logic to do this is fairly straightforward:

  • Define the maximum value in the ProgressBar, say 100 as you have it there.
  • If the max value is 100, then assign the first value to a random number between 1 and 100.
  • Every time you push the button, get a random number. If that number is higher than the current value, get another one (or exit if you prefer).
  • Set the value to the current value minus the random number.
  • If the current value is 1, quit.


It doesn't look like you've done this anywhere that I can see, including in your "final result". I suspect that you've been getting errors because you are failing to check whether your generated random number is greater than the current value of the ProgressBar. This becomes an increasingly likely possibility as you continue to adjust the value downward. For example, your "final result" will throw an exception if your current ProgressBar value is, say, 4 and your current random number is, say, -9. I don't believe your code as it stands will avoid this problem.

This post has been edited by BobRodes: 21 June 2015 - 10:30 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1