Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Unexpected outcome with for loop and rand()

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unexpected outcome with for loop and rand()

    Can anyone explain why the results of this loop does not result in a list with 365 values?

    Code:
    dim vnlc as N
    dim vLodds as C
    dim vrand as C=""
    for i = 1 to 365
    		
    	RANDODD:	
    	vrand=(""+int(rand()*1000))
    		'use only odd values
    		if mod(val(vrand),2)=1
    			'assure the value is unique
    			if vrand $ vLodds
    				GOTO RANDODD
    			end if
    		
    			vLodds=vLodds+PADL(vrand,3,"0")+crlf()
    
    		end if
    next
    vnlc=line_count(vLodds)
    msgbox("ODDS: "+(""+vnlc),vLodds)
    end
    Mike W
    __________________________
    "I rebel in at least small things to express to the world that I have not completely surrendered"

  • #2
    Re: Unexpected outcome with for loop and rand()

    'use only odd values
    There can be only one.

    Comment


    • #3
      Re: Unexpected outcome with for loop and rand()

      the code gave me even number too, so I removed the code.
      Last edited by GGandhi; 10-04-2016, 02:44 PM.
      thanks for reading

      gandhi

      version 11 3381 - 4096
      mysql backend
      http://www.alphawebprogramming.blogspot.com
      [email protected]
      Skype:[email protected]
      1 914 924 5171

      Comment


      • #4
        Re: Unexpected outcome with for loop and rand()

        Why?
        Multiple reasons.
        See if you could decipher those reasons by looking at the following modifications which produce 365 every time:
        Code:
        dim vLodds as C
        dim vrand as C=""
        
        for i = 1 to 365
        		
        	RANDODD:	
        	vrand=(""+int(rand()*1000))
        		'use only odd values
        		if mod(val(vrand),2)=1
        			vrand=PADL(vrand,3,"0")
        			'assure the value is unique
        			if vrand $ vLodds
        				GOTO RANDODD
        				else
        				vLodds=vLodds+PADL(vrand,3,"0")+crlf()
        			end if
        		else
        			GOTO RANDODD
        
        		end if
        next
        vnlc=line_count(vLodds)
        msgbox(""+vnlc)
        'msgbox("ODDS: "+(""+vnlc),vLodds)
        end

        Comment


        • #5
          Re: Unexpected outcome with for loop and rand()

          Alright, I will make it easy:
          Reason #1:
          You are comparing apples to oranges:
          You are looking to see if vrand exists in vLodds
          If it does, you go back to RANDODD
          If it does not ..well here is the first mistake..you don't just add it, you add padl(...vrand)..it may or may not be the same as you are generating random numbers from 1 to 1000
          The modification I made is to change vrand to PADL(..vrand..) and then look up to see if it exists

          Reason #2:
          You didn't tell the script what to do when it generates an even random number. So, it discards the even number and goes on to the for loop, which means, i go up by one and you exhaust the 365 loops before generating 365 odd random numbers.
          The modification I made: go back to RANDODD without looping through the for loop thus i does not change until an odd number is generated.

          Comment


          • #6
            Re: Unexpected outcome with for loop and rand()

            Code:
            dim vnlc as N = 0
            dim vLodds as C = ""
            dim vrand as C=""
            for i = 1 to 365
            		
            	RANDODD:	
            	vrand=(""+int(rand()*1000))
            		'use only odd values
            				if (mod(val(vrand),2)=0) 
            				GOTO RANDODD
            				end if
            
            				if vrand $ vLodds
            				goto RANDODD
            				end if
            		vLodds=vLodds+PADL(vrand,3,"0")+crlf()
            
            		
            next
            vnlc=line_count(vLodds)
            msgbox("ODDS: "+(""+vnlc),vLodds)
            end
            I did this way and it does produce 365 every time with odd values.

            edit:
            Gabriel has posted better looking code, just noticed.
            thanks for reading

            gandhi

            version 11 3381 - 4096
            mysql backend
            http://www.alphawebprogramming.blogspot.com
            [email protected]
            Skype:[email protected]
            1 914 924 5171

            Comment


            • #7
              Re: Unexpected outcome with for loop and rand()

              I've never been fond of goto.

              Code:
              dim vnlc as N = 1
              dim vLodds as C = ""
              dim vrand as C=""
              while vnlc < 366
              		
              vrand=(""+int(rand()*1000))
              		'use only odd values
              		if mod(val(vrand),2)=1
              			'assure the value is unique
              			if vrand !$ vLodds
              				vLodds=vLodds+PADL(vrand,3,"0")+crlf()
              				vnlc = vnlc + 1
              			end if
              		end if
              end while
              vnlc=line_count(vLodds)
              msgbox("ODDS: "+(""+vnlc),vLodds)
              end
              There can be only one.

              Comment


              • #8
                Re: Unexpected outcome with for loop and rand()

                Only use odd numbers. Don't define what to do with even numbers. OK yes, I do feel a bit humiliated Thanks for all the comments. though.
                Mike W
                __________________________
                "I rebel in at least small things to express to the world that I have not completely surrendered"

                Comment


                • #9
                  Re: Unexpected outcome with for loop and rand()

                  Let make a corrections here:
                  The part about comparing apples to oranges that I mentioned earlier is not correct. Take a look at this:
                  Code:
                  ?"3" $ "003"
                  = .T.
                  But then you would have to wonder, if that's true, then any value that has 3 in it will be deemed as existing, which means, at best you could generate only 9 values even if you run the script until the cows come home.
                  So, how come that does not happen?
                  Your script as is does generate 100 semi odd values. How so?
                  Well, the values generated will mostly be 2 or 3 digits making it very hard to exist in another value, but then there is another reason:
                  rand() will generate a series, all the values in that series are unique.
                  With that said, why bother looking for an existing number?!
                  By checking using the $ function, it might be counter productive since it will deem 57 as existing if the list has, say 573.
                  Counterproductive in the sense that you will have to run the rand() one more time unnecessarily.

                  Now, if you want to be paranoid about this, you could keep the series as numeric (perhaps in an array) and after you make the comparison, convert to character.
                  Last edited by G Gabriel; 10-04-2016, 03:59 PM.

                  Comment


                  • #10
                    Re: Unexpected outcome with for loop and rand()

                    G,
                    Yeah, I took the route of doing the conversion to a three character character value (vrand=padl(""+int(rand()*1000)),3,"0") before checking for if it already exists. Thanks for sharing your insight!
                    Mike W
                    __________________________
                    "I rebel in at least small things to express to the world that I have not completely surrendered"

                    Comment


                    • #11
                      Re: Unexpected outcome with for loop and rand()

                      Not a fan of GoTO either. I only modified Mike's script to show where things went wrong, but if you want a more concise script, here is a function that will produce a specific and unique number of either odd or even values:
                      Code:
                      FUNCTION Rand_O_E AS C (O_E AS C, Number AS N, end as n)
                      dim lst as c=""
                      dim inc as n=1
                      	if O_E="O"
                      	      inc=0
                      	end if
                      for i=1 to number
                      x=int(rand()*end)
                      x=if(mod(x,2)=inc,x+1,x)
                      UniqueNameGet(lst,""+x)
                      lst=lst+x+crlf()
                      Rand_O_E=lst
                      next
                      END FUNCTION
                      Legend:
                      O_E: Odd or Even
                      Number: Number of values
                      End: Random number between 1 and "end"

                      Example1:
                      Produce 4 even values between 1 and 100:
                      Code:
                      ?rand_O_E("e",4,100)
                      = 48
                      72
                      44
                      12
                      Example 2:
                      Produce 6 odd values between 1 and 1000
                      Code:
                      ?rand_O_E("o",6,1000)
                      = 703
                      639
                      147
                      267
                      709
                      259

                      Comment

                      Working...
                      X