473,651 Members | 3,012 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enumerations And Random Numbers

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/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
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))
--

Nov 21 '05 #1
38 2298
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
"One Handed Man ( OHM - Terry Burns )" <news.microsoft .com>
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/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
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))
--

Nov 21 '05 #2
"One Handed Man ( OHM - Terry Burns )" <news.microsoft .com> schrieb:
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(GetT ype(AnchorStyle s))
Dim r As New Random
MsgBox(Values(r .Next(0, Values.Length - 1)).ToString())
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #3

"One Handed Man ( OHM - Terry Burns )" <news.microsoft .com> wrote
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
Nov 21 '05 #4
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.ne xt( 0, MyEnumeration.M ax )

See what I mean, I mat have missed something here

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

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
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))
--
"Larry Serflaten" <se*******@usin ternet.com> wrote in message
news:eb******** ******@TK2MSFTN GP10.phx.gbl...

"One Handed Man ( OHM - Terry Burns )" <news.microsoft .com> wrote
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

Nov 21 '05 #5

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote
Dim Values() As AnchorStyles = [Enum].GetValues(GetT ype(AnchorStyle s))
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(GetT ype(AnchorStyle s)), AnchorStyles())
Dim r As New Random
MsgBox(Values(r .Next(0, Values.Length)) .ToString())

LFS
Nov 21 '05 #6
"Larry Serflaten" <se*******@usin ternet.com> schrieb:
Dim Values() As AnchorStyles = [Enum].GetValues(GetT ype(AnchorStyle s))
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(GetT ype(AnchorStyle s)), AnchorStyles())
Dim r As New Random
MsgBox(Values(r .Next(0, Values.Length)) .ToString())


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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #7

"Mr Newbie" <NO********@THI SADDRESS.COM> wrote
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() {TravelDirectio n.North, TravelDirection .South, _
TravelDirection .East, TravelDirection .West}

test = pick(Rnd.Next(0 , 4))
;-)
LFS
Nov 21 '05 #8
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/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
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 [MVP]" <hi************ ***@gmx.at> wrote in message
news:uT******** *****@TK2MSFTNG P11.phx.gbl...
"Larry Serflaten" <se*******@usin ternet.com> schrieb:
Dim Values() As AnchorStyles = [Enum].GetValues(GetT ype(AnchorStyle s))
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(GetT ype(AnchorStyle s)), AnchorStyles())
Dim r As New Random
MsgBox(Values(r .Next(0, Values.Length)) .ToString())


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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #9
Cheers Cor

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

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
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" <no************ @planet.nl> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
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
"One Handed Man ( OHM - Terry Burns )" <news.microsoft .com>
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/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
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))
--


Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
3008
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to generate a set of random number that follow a normal distribution N(0,1) ? I am develloping on power4 machine running AIX. Thank you for your help
4
3530
by: ChrisB | last post by:
Hello: I will be creating 50+ enumerations related to a large number of classes that span a number of namespaces. I was wondering if there are any "best practices" when defining enumerations. Should a single class be created that contains all of a solution's enumerations? Or should enumerations be defined directly in the files that contain the related classes? Are there any other design patterns I am overlooking?
27
2661
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to provide a "one obvious way" to do enumerations in Python. > >>> from enum import Enum > >>> day = Enum('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')
104
5126
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 better way than to fill an array of range 0... RAND_MAX with pre-computed primes and using the output of rand() to index into it to extract a random prime.
77
3908
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the reStructuredText source as it is today. Please discuss it here so I can see what issues people may have.
12
5218
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 RAND(). any ideas? I suppose I could use the current rancom number as the seed for the next random number. but would that really work?
13
2799
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000, or between 0 and 99999 (inclusive). Further, the I want the range to be a variable. Concretely, I would like to create the following method: unsigned long Random( unsigned long num )
6
11741
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 that will randomly generate 10 unique random numbers. Your job is to write a program that produces random permutations of the numbers 1 to 10. “Permutation” is a mathematical name for an arrangement. For example, there are six permutations of the...
24
7204
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me uniformly distributed and unique numbers ?
0
8278
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8807
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8584
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.