Raised This Month: $159 Target: $400
 39% 

Pick random value from enum


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
nhnkl159
Senior Member
Join Date: Jul 2012
Location: Israel 3>
Old 10-11-2016 , 08:04   Pick random value from enum
Reply With Quote #1

Is it possible ?

enum example :

Code:
enum Days(+=1)
{
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
}
__________________

Last edited by Fyren; 10-11-2016 at 09:11. Reason: don't put random links in your post
nhnkl159 is offline
Send a message via Skype™ to nhnkl159
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 10-11-2016 , 09:13   Re: Pick random value from enum
Reply With Quote #2

By default, enum values start from zero and increment for each. You can pick a random number from 0 to 6 and use it as a value for your enum, though you might have to manually tag/view_as to avoid warnings.
Fyren is offline
thecount
Veteran Member
Join Date: Jul 2013
Old 10-11-2016 , 16:48   Re: Pick random value from enum
Reply With Quote #3

For enums of varying or long lengths, you may also add an element at the end of the list to represent the amount of elements before it to help with looping and such. Like:
PHP Code:
enum type{
enum1//0
enum2//1
enum3//2
TOTAL_ENUM 3

For your situation, I think you would just want something like
PHP Code:
view_as<Days>:GetRandomInt(MondaySunday); 
or whatever the syntax is for that. I don't cast variables to different types very often in SourcePawn.

Last edited by thecount; 10-11-2016 at 16:51.
thecount is offline
nhnkl159
Senior Member
Join Date: Jul 2012
Location: Israel 3>
Old 10-12-2016 , 13:19   Re: Pick random value from enum
Reply With Quote #4

Quote:
Originally Posted by thecount View Post
For enums of varying or long lengths, you may also add an element at the end of the list to represent the amount of elements before it to help with looping and such. Like:
PHP Code:
enum type{
enum1//0
enum2//1
enum3//2
TOTAL_ENUM 3

For your situation, I think you would just want something like
PHP Code:
view_as<Days>:GetRandomInt(MondaySunday); 
or whatever the syntax is for that. I don't cast variables to different types very often in SourcePawn.
Code:
int day = view_as<Days>(GetRandomInt(Monday, Sunday));
Got 2 same warnings on this line :
warning 213: tag mismatch
warning 213: tag mismatch
__________________
nhnkl159 is offline
Send a message via Skype™ to nhnkl159
shavit
AlliedModders Donor
Join Date: Dec 2011
Location: Israel
Old 10-12-2016 , 15:09   Re: Pick random value from enum
Reply With Quote #5

Code:
enum Day
{
	Monday,
	Tuesday,
	Wednesday,
	Thursday,
	Friday,
	Saturday,
	Sunday,
	MAX_DAYS
}

Day GetRandomDay()
{
	return view_as<Day>(GetRandomInt(0, view_as<int>(MAX_DAYS) - 1));
}
__________________
retired

Last edited by shavit; 10-13-2016 at 04:19. Reason: fix out of bounds issue
shavit is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 10-12-2016 , 23:56   Re: Pick random value from enum
Reply With Quote #6

Code:
enum Day
{
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
}

// int iDay = GetRandomInt( 0, view_as < int > ( Day ) - 1 );
// Day dayRandom = view_as < Day > ( GetRandomInt( 0, view_as < int > ( Day ) - 1 ) );

Day RandomDay()
{
    return view_as < Day > ( GetRandomInt( 0, view_as < int > ( Day ) - 1 );
}
// Day dayRandom = RandomDay();

Day NextDay( Day dayStart = Sunday )
{
    return ( ++dayStart ) % Day;
}
// Day dayNext = NextDay( Saturday );
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:18.


Powered by vBulletin®
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Theme made by Freecode