Enumerations And Random Numbers

  • Thread starter One Handed Man \( OHM - Terry Burns \)
  • Start date
O

One Handed Man \( OHM - Terry Burns \)

I have a basic question thats been niggling me, but I never found time to
look at it before.


If I have an enumeration such as this

Fiend Enum TravelDirection
North
South
East
West
End Enum

and

Direction = TravelDirection


Now I want to set the Direction Randomly to one of the TravelDirection enum
values

If I know that my enum is between 1 and 4 I can easily get a new random
number and assign it, however, As I add to the enumeration, I wont want to
worry about coding all the changes, so how could I easily do this I know I
can do it, and I have but Im looking for the most simple solution. There
doesent seem to be a Max property for enumerations.

If this sounds dumb, then I appologise












--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
 
C

Cor Ligthert

Hi Terry,

Is there a reason that it seems that you do it for me difficult? I think I
miss something.

Two times the last position of environment tickcount + 5 divided by 5 should
in my opinion do the trick.

Cor
 
H

Herfried K. Wagner [MVP]

One Handed Man ( OHM - Terry Burns ) said:
If I have an enumeration such as this

Fiend Enum TravelDirection
North
South
East
West
End Enum

and

Direction = TravelDirection

Now I want to set the Direction Randomly to one of the TravelDirection
enum values

If I know that my enum is between 1 and 4 I can easily get a new random
number and assign it, however, As I add to the enumeration, I wont want to
worry about coding all the changes, so how could I easily do this I know
I can do it, and I have but Im looking for the most simple solution.

\\\
Dim Values() As AnchorStyles = [Enum].GetValues(GetType(AnchorStyles))
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length - 1)).ToString())
///
 
L

Larry Serflaten

One Handed Man ( OHM - Terry Burns ) said:
I have a basic question thats been niggling me, but I never found time to
look at it before.


If I have an enumeration such as this

Fiend Enum TravelDirection
North
South
East
West
End Enum

and

Direction = TravelDirection


That should be:

Direction = TravelDirection.East

Or some other specific value. You wouldn't be able to assign the
entire enum to a variable.
Now I want to set the Direction Randomly to one of the TravelDirection enum
values

If I know that my enum is between 1 and 4 I can easily get a new random
number and assign it, however, As I add to the enumeration, I wont want to
worry about coding all the changes, so how could I easily do this I know I
can do it, and I have but Im looking for the most simple solution. There
doesent seem to be a Max property for enumerations.

Why not use one of the values?
East
West MaxDirection = West
End Enum

LFS
 
M

Mr Newbie

Sorry, I think I F*c(ed up here.

What I meant was how to set the variable to a Random number somwhere in the
range of the enumeration.

So Direction = ( Some Random Number In the Range )

I dont want to explicitly set it like this

Direction = RandomNumber( 0, 10)

I was looking for something more like

Direction = RandomNumber.next( 0, MyEnumeration.Max )

See what I mean, I mat have missed something here



--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
L

Larry Serflaten

Herfried K. Wagner said:
Dim Values() As AnchorStyles = [Enum].GetValues(GetType(AnchorStyles))
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length - 1)).ToString())

The maxValue of Next needs to be one greater than the last value desired:

Dim Values() As AnchorStyles = CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles())
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length)).ToString())

LFS
 
H

Herfried K. Wagner [MVP]

Larry Serflaten said:
Dim Values() As AnchorStyles = [Enum].GetValues(GetType(AnchorStyles))
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length - 1)).ToString())

The maxValue of Next needs to be one greater than the last value desired:

Dim Values() As AnchorStyles =
CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles())
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length)).ToString())

ACK, I should have read the documentation... ;-).
 
L

Larry Serflaten

Mr Newbie said:
What I meant was how to set the variable to a Random number somwhere in the
range of the enumeration.

So Direction = ( Some Random Number In the Range )

As Herfried shows, the best solution is to add the members to an array, and
pick some random value out of the array. It may be that the values in the Enum
are NOT sequential, so that you can't just pick some number from 0 to some
max value. With the actual Enum values in an array, you can be sure picking
one element of the array will give you some value that is actually in the Enum.

Make sense?

BTW, another route would be something like this:

Dim pick As Integer() = New Integer() {TravelDirection.North, TravelDirection.South, _
TravelDirection.East, TravelDirection.West}

test = pick(Rnd.Next(0, 4))


;-)
LFS
 
O

One Handed Man \( OHM - Terry Burns \)

Thanks 2 both, i've used something similar in the past but its just a shame
this was not built in to an enum class.

Cheers

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Herfried K. Wagner said:
Larry Serflaten said:
Dim Values() As AnchorStyles = [Enum].GetValues(GetType(AnchorStyles))
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length - 1)).ToString())

The maxValue of Next needs to be one greater than the last value desired:

Dim Values() As AnchorStyles =
CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles())
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length)).ToString())

ACK, I should have read the documentation... ;-).
 
O

One Handed Man \( OHM - Terry Burns \)

Cheers Cor

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Cor Ligthert said:
Hi Terry,

Is there a reason that it seems that you do it for me difficult? I think
I miss something.

Two times the last position of environment tickcount + 5 divided by 5
should in my opinion do the trick.

Cor
 
O

One Handed Man \( OHM - Terry Burns \)

Very true, I didnt think of that, yes they may not be sequentional good
point. However, it would however, be nice if this was built into the enum
class.


--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
J

Jay B. Harlow [MVP - Outlook]

OHM,
??

You want a method on the Enum class that randomly returns one of the Enum's
values?

IMHO Such a function would suggest that the Enum class is trying to do too
much!

I would use code similar to Herfried's in a context appropriate class,
probably the class that was consuming the random enum value.

Hope this helps
Jay

One Handed Man ( OHM - Terry Burns ) said:
Thanks 2 both, i've used something similar in the past but its just a
shame this was not built in to an enum class.

Cheers

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Herfried K. Wagner said:
Larry Serflaten said:
Dim Values() As AnchorStyles = [Enum].GetValues(GetType(AnchorStyles))
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length - 1)).ToString())

The maxValue of Next needs to be one greater than the last value
desired:

Dim Values() As AnchorStyles =
CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles())
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length)).ToString())

ACK, I should have read the documentation... ;-).
 
O

One Handed Man \( OHM - Terry Burns \)

OK, why do you think its too much to ask of an Enum Class to be able to
report the number of elements it contains ?

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Jay B. Harlow said:
OHM,
??

You want a method on the Enum class that randomly returns one of the
Enum's values?

IMHO Such a function would suggest that the Enum class is trying to do too
much!

I would use code similar to Herfried's in a context appropriate class,
probably the class that was consuming the random enum value.

Hope this helps
Jay

One Handed Man ( OHM - Terry Burns ) said:
Thanks 2 both, i've used something similar in the past but its just a
shame this was not built in to an enum class.

Cheers

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Herfried K. Wagner said:
Dim Values() As AnchorStyles = [Enum].GetValues(GetType(AnchorStyles))
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length - 1)).ToString())

The maxValue of Next needs to be one greater than the last value
desired:

Dim Values() As AnchorStyles =
CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles())
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length)).ToString())

ACK, I should have read the documentation... ;-).
 
C

Cor Ligthert

Terry,

I hope you do not mind that I answer,

In my opinion is an Enum the best used when it has the values
1
2
4
8
Than you can do an OR or whatever with it, and has it no sence to know the
max value (in this case 15) or F

However just my opinion.

Cor


One Handed Man ( OHM - Terry Burns ) said:
OK, why do you think its too much to ask of an Enum Class to be able to
report the number of elements it contains ?

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Jay B. Harlow said:
OHM,
??

You want a method on the Enum class that randomly returns one of the
Enum's values?

IMHO Such a function would suggest that the Enum class is trying to do
too much!

I would use code similar to Herfried's in a context appropriate class,
probably the class that was consuming the random enum value.

Hope this helps
Jay

One Handed Man ( OHM - Terry Burns ) said:
Thanks 2 both, i've used something similar in the past but its just a
shame this was not built in to an enum class.

Cheers

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Dim Values() As AnchorStyles =
[Enum].GetValues(GetType(AnchorStyles))
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length - 1)).ToString())

The maxValue of Next needs to be one greater than the last value
desired:

Dim Values() As AnchorStyles =
CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles())
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length)).ToString())

ACK, I should have read the documentation... ;-).
 
O

One Handed Man \( OHM - Terry Burns \)

Hi Cor,
Im not sure if you understood my origional post. What I wanted
was to be able to choose randomly any element from the Enum members, in
order to do this you need to know the number of elements.

Herfried's code will do this quite well.

I went on to say that It would be nice if the Enum class would be able to
choose one element at Random and Jay said he thought this was to much and it
probably is, however, it would be nice if the Enum could report the number
of elements it has to simplify choosing one from its return element values
array.

Unfortunately you dont seem to be able to inherit an Enum or I would have
wrapped it myself.

Cheers

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Cor Ligthert said:
Terry,

I hope you do not mind that I answer,

In my opinion is an Enum the best used when it has the values
1
2
4
8
Than you can do an OR or whatever with it, and has it no sence to know the
max value (in this case 15) or F

However just my opinion.

Cor


One Handed Man ( OHM - Terry Burns ) said:
OK, why do you think its too much to ask of an Enum Class to be able to
report the number of elements it contains ?

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Jay B. Harlow said:
OHM,
??

You want a method on the Enum class that randomly returns one of the
Enum's values?

IMHO Such a function would suggest that the Enum class is trying to do
too much!

I would use code similar to Herfried's in a context appropriate class,
probably the class that was consuming the random enum value.

Hope this helps
Jay

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in
message Thanks 2 both, i've used something similar in the past but its just a
shame this was not built in to an enum class.

Cheers

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Dim Values() As AnchorStyles =
[Enum].GetValues(GetType(AnchorStyles))
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length - 1)).ToString())

The maxValue of Next needs to be one greater than the last value
desired:

Dim Values() As AnchorStyles =
CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles())
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length)).ToString())

ACK, I should have read the documentation... ;-).
 
H

Herfried K. Wagner [MVP]

Terry,

One Handed Man ( OHM - Terry Burns ) said:
I went on to say that It would be nice if the Enum class
would be able to choose one element at Random and Jay
said he thought this was to much and it probably is

In this particular case, I agree with Jay. Picking a random enumeration
constant doesn't have anything to do with enums and thus should not be
added.
however, it would be nice if the Enum could report the number of elements
it has

I agree up to this point that this would be a nice addition to enumerations.
 
J

Jay B. Harlow [MVP - Outlook]

OHM,
OK, why do you think its too much to ask of an Enum Class to be able to
report the number of elements it contains ?

That's not what I stated! :-|

I think having the Enum Class report the number of elements an enum contains
might be a good thing. Although you can easily get the number from
Enum.GetNames or Enum.GetValues. I say "might be a good thing", as it could
be too easy to introduce problems as Enum values are not required to be
contiguous, nor start with zero.


What I said would be a bad thing, and I attempted to be very specific, is
for System.Enum to have a function that would return a random value (ala
Herfried's code). Yes, your code needs it, however is Returning a random
value really a realistic behavior that all Enums should behave? I don't see
that it is, hence I don't think Enum should possess that ability.


Now I do agree it would be nice to add our own functions to specific enums,
such as your TravelDirection enum might have the ability to return a random
value. Unfortunately the designers at MS decided not to allow that.

Hope this helps
Jay



One Handed Man ( OHM - Terry Burns ) said:
OK, why do you think its too much to ask of an Enum Class to be able to
report the number of elements it contains ?

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
<<snip>>
 
L

Larry Serflaten

One Handed Man ( OHM - Terry Burns ) said:
it would be nice if the Enum could report the number
of elements it has to simplify choosing one from its return element values
array.


But Herfried has already shown how to do that. When you put the elements in
an array, the array itself will tell you how many there are. Or, you can get the
value from the Enum class:

Dim cnt As Integer = [Enum].GetValues(GetType(MyEnum)).Length
Debug.WriteLine(cnt)


I don't see how they could add anything else. What if you wanted to have
a member called Length? If it was used as a shared member of your enum,
you couldn't use it:

cnt = MyEnum.Length ' ???

Do you see how they really shouldn't add anything to your enum except what
you list as members of the enum?

LFS
 
J

Jay B. Harlow [MVP - Outlook]

Larry,
I could see a Enum.GetLength function:
Dim cnt As Integer = [Enum].GetLength(GetType(MyEnum))

That would not need to actually create & return the array of values.

However!!! This could cause problems, as Enums are not required to start
with zero nor are they required to be contiguous.

In other words, code such as:

Public Enum MyEnum
None = 0
Value1 = 55
Value2 = 65
Value3 = 75
End Enum

'For value As MyEnum = 0 to [Enum].GetLength(GetType(MyEnum))
For value As MyEnum = 0 to [Enum].GetValues(GetType(MyEnum)).Length
Debug.WriteLine(value, value.ToString())
Next

Would cause all sorts of mischief.

Where as this one does not:

For Each value As MyEnum In [Enum].GetValues(GetType(MyEnum))
Debug.WriteLine(value, value.ToString())
Next


Hope this helps
Jay


Larry Serflaten said:
One Handed Man ( OHM - Terry Burns ) said:
it would be nice if the Enum could report the number
of elements it has to simplify choosing one from its return element
values
array.


But Herfried has already shown how to do that. When you put the elements
in
an array, the array itself will tell you how many there are. Or, you can
get the
value from the Enum class:

Dim cnt As Integer = [Enum].GetValues(GetType(MyEnum)).Length
Debug.WriteLine(cnt)


I don't see how they could add anything else. What if you wanted to have
a member called Length? If it was used as a shared member of your enum,
you couldn't use it:

cnt = MyEnum.Length ' ???

Do you see how they really shouldn't add anything to your enum except what
you list as members of the enum?

LFS
 
J

JD

For one, enum per CLI specification cannot have any
methods\properties\events of its own. It can't even have any instance fields
except one, and thats used for defining the underlying type of the enum. So
forget about extending it.

Could it add a shared method Count that excepts an enum type and returns the
number of fields? Maybe, but I'm not sure it would be a good design
decision.

Lets say it has the shared Count\Length method, Count\Length denotes
arrays\collections\containers, and enum is not any of these. Matter of fact
an enum does not contain anything except the one instance variable as
perviously mentioned.

Could it represent the size of the enum? No because an enum defines named
literals and thats it.

Could it represent the number of valid values of the enum? Not really
because I can assign any value to an enum field whether its defined by the
enum type or not. Ex.
Public Enum Fruit
Apple
Orange
End Enum
Dim F As Fruit
F = Ctype(10,Fruit)

What it actually represents is the number of named literals defined by the
enum type. If this is true then what good is this value when used against
the enum itself? What does it tell you about the enum and how is it useful?
I can't really think of anything useful off the top of my head.

Where it does become useful is when you ask for the name or value arrays,
and arrays already of the property length, and it properly describes the
array not the enum

Anyways thinking out loud and just my two cents

JD



One Handed Man ( OHM - Terry Burns ) said:
OK, why do you think its too much to ask of an Enum Class to be able to
report the number of elements it contains ?

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Jay B. Harlow said:
OHM,
??

You want a method on the Enum class that randomly returns one of the
Enum's values?

IMHO Such a function would suggest that the Enum class is trying to do too
much!

I would use code similar to Herfried's in a context appropriate class,
probably the class that was consuming the random enum value.

Hope this helps
Jay

One Handed Man ( OHM - Terry Burns ) said:
Thanks 2 both, i've used something similar in the past but its just a
shame this was not built in to an enum class.

Cheers

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Dim Values() As AnchorStyles = [Enum].GetValues(GetType(AnchorStyles))
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length - 1)).ToString())

The maxValue of Next needs to be one greater than the last value
desired:

Dim Values() As AnchorStyles =
CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles())
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length)).ToString())

ACK, I should have read the documentation... ;-).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

OT - Good Ideas ? 3
Threading 16
This Group, what more do we need ? 19
Threading Performance 7
Why Does This Fail ( Threading ) 17
My Turn - Threading 10
How to tell if a document is new ? 5
Details Vs Thumnails 7

Top