For:Next:Step and random() (last update: 08/11/15)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

For:Next:Step and random() (last update: 08/11/15)

Post by DK_PETER »

I would be nice to use a variable as a step value like this:

Code: Select all

Procedure ForNextNewStepValue(Interval.i)
  Protected x.i
  For x = 0 To 100 Step Interval   ;this would be a nice change
    Debug "Interval won't work. Must be a numeric constant"
  Next x
EndProcedure
For now you can do something like this:

Code: Select all

Procedure ForNextNewStepValue(Interval.i)
  Protected x.i
  Select Interval
    Case 2
      For x = 0 To 100 Step 2
        Debug "It's a numeric constant"
      Next x
    Case 3
      For x = 0 To 100 Step 3   
        Debug "It's a numeric constant"
      Next x
    Default
  EndSelect   
EndProcedure
Next I think Random() needs an update:

Something like this would be nice:

Code: Select all

Random(200, -200, #PB_Integer/#PB_Float)  ;if you wish to use integer or float
For now you have to do something like this:

Code: Select all

Procedure.i RandomI(min.i, Max.i, SeedVal.i = #PB_Ignore)
  If SeedVal = #PB_Ignore : SeedVal = ElapsedMilliseconds() : EndIf
  ProcedureReturn (Min + (Max - Min) * Random(SeedVal) / SeedVal)
EndProcedure

Procedure.f RandomF(min.f, Max.f, SeedVal.i = #PB_Ignore)
  If SeedVal = #PB_Ignore : SeedVal = ElapsedMilliseconds() : EndIf
  ProcedureReturn (Min + (Max - Min) * Random(SeedVal) / SeedVal)
EndProcedure

Debug RandomI(-20, 20)
Debug RandomF(-20.5, 20.5)
EDIT:
It would surely also be nice if you could use structured variables.

Something like this:

Code: Select all

Structure _Counters
  x.i
  y.i
  z.i
EndStructure

Global co._Counters

For co\x = 0 To 500
  
Next co\x
Last edited by DK_PETER on Fri Aug 14, 2015 11:18 am, edited 2 times in total.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: For:Next:Step and random()

Post by Danilo »

DK_PETER wrote:For now you can do something like this:

Code: Select all

Procedure ForNextNewStepValue(Interval.i)
  Protected x.i
  Select Interval
    Case 2
      For x = 0 To 100 Step 2
        Debug "It's a numeric constant"
      Next x
    Case 3
      For x = 0 To 100 Step 3   
        Debug "It's a numeric constant"
      Next x
    Default
  EndSelect   
EndProcedure
Wouldn't a While-loop be better for now?

Code: Select all

Procedure ForNextNewStepValue(stepValue.i=1)
  Protected x.i = 0                           ; For x = 0 to 100 Step stepValue
  While x <= 100
      Debug x                                 ;     Debug x
      x + stepValue
  Wend                                        ; Next
  Debug x                                     ; Debug x
EndProcedure

ForNextNewStepValue(3)
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: For:Next:Step and random()

Post by DK_PETER »

@Danilo
Yes, a While:Wend is absolutely doable.
I still think, that using a variable as a step value in a For:Next loop would be a really nice change, though. :-)
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: For:Next:Step and random()

Post by Danilo »

I was responding to your "For now you can do something like this". Didn't say a variable step wouldn't be useful. ;)
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: For:Next:Step and random()

Post by IdeasVacuum »

using a variable as a step value in a For:Next loop would be a really nice change
+1 (it's been requested on the forum for years now)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply