Jump to content
Sign in to follow this  
jebus495

Random (1, 1) doesn't always = 1?

Recommended Posts

jebus495

I tried searching but found nothing about this. The goal here is for the user of my program (this is not the full script) to be able to set how often this function happens.

I understand what I have done here might be a bit of a roundabout way of accomplishing this so any alternatives would be good. :)

It seems odd to me that $gotofish never = 1 even when "fishsometimes is set to 1.

Perhaps I have made a silly mistake?

CODE
Func gotofish()

$dontfishafterlvl = 0

$fishsometimes = IniRead("NAMEOFINI.ini", "Values", "fishsometimes", "NONE SET")

$gotofish = Random (1, $fishsometimes)

If $gotofish = 1 Then

MouseMove(260, 350)

Sleep(1000)

MouseClick("Left")

Sleep(1000)

MouseMove(200, 375)

Sleep(1000)

MouseClick("left")

Sleep(1000)

fish()

Else

gotoarena()

EndIf

EndFunc ;==>gotofish

Share this post


Link to post
Share on other sites
Skruge

It's not explicitly stated in the documentation, but Min must be less than Max.

If they're equal, as in your case, Random returns 0 and sets @error to 1.

I don't know if this is what you're trying to accomplish, but I think this is what you want based on the name of your variable...

$gotofish = Random (0, $fishsometimes, 1)

If $fishsometimes = 0, the result will always be 0. (Because the inputs are invalid, not because the range is 0-0)

If $fishsometimes = 1, the result can be 0 or 1. (Range: 0-1)

Note: Setting the third parameter (Flag) to 1 ensures that you don't get a fraction between 0 and 1.


[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites
Melba23

jebus495,

Ran into the same problem a while ago. In the end I decided that I had to code around it:

; Get a random number
If $aIndex_Array[0] = 1 Then
; If only 1 entry, Random will return 0 so force 1
    $iRandom_Choice = 1
Else
; Get a random integer within the index bounds
    $iRandom_Choice = Random(1, $aIndex_Array[0], 1)
EndIf

There was an attempt to change this behaviour, but the Devs said "No" in no uncertain terms!

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Spiff59

Of course he could avoid triggering an @error condition with this variation:

$gotofish = $fishsometimes * Random (0, 1, 1)

Share this post


Link to post
Share on other sites
jebus495

Thanks for all the responses.

I went with $gotofish = Random (1, $fishsometimes,1)

Basically just because the way I have my config set up. 1 = on 0 = off.

Now 0 = off

1 = on

2 = half on

3 = 1/4 on

4 = 1/8 on and so on :)

Almost everything in the program I have written needs to be at least a little random or I'll get "caught" :lmao:

Share this post


Link to post
Share on other sites
Spiff59

If you need the 0 through 4 codes, this does what you want in a single line:

While 1
    $fishingfrequency = InputBox("","Input fishingfrequency (0-4, enter to exit): "); 0=Never, 1=25%, 2=50%, 3=75%, 4=Always
    If $fishingfrequency = "" then ExitLoop
    $j = 0
    For $i = 1 to 1000
        $gotofish = ($fishingfrequency * .25) > Random() 
        If $gotofish Then
            $j += 1; Went fishing
        EndIf
    Next
    MsgBox(1,"","With fishingfrequency set to " & $fishingfrequency & ", you'll go fishing " & $j & " times out of 1000" )
WEnd

Or, if you don't really require the 0-4 stuff, just set your variable to whatever percent you desire:

While 1
    $fishingfrequency = InputBox("","Input fishingfrequency (0-100%, enter to exit): ") 
    If $fishingfrequency = "" then ExitLoop
    $j = 0
    For $i = 1 to 1000
        $gotofish = ($fishingfrequency / 100) > Random() 
        If $gotofish Then
            $j += 1; Went fishing
        EndIf
    Next
    MsgBox(1,"","With fishingfrequency set to " & $fishingfrequency & ", you'll go fishing " & $j & " times out of 1000" )
WEnd
Edited by Spiff59

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.