GlowScript math: random functions? How??

447 views
Skip to first unread message

Anton Magursky

unread,
Jan 13, 2021, 4:44:12 PM1/13/21
to Glowscript Users

Hi! The documentation says "seed_state, get_random_byte, seed, random, randrange, randint, uniform, choice, shuffle, and sample have been implemented in GlowScript."
However I was not able to use any of those in my program, except "random()", which produces float number from 0 to 1.
There are no examples on how to use "choice" for a list :( I tried many ideas. But this is just not working. Please help me (and also consider if possible to add this into documentation).
Thank you!

Bruce Sherwood

unread,
Jan 13, 2021, 5:10:45 PM1/13/21
to Glowscript Users
From the documentation (https://www.glowscript.org/docs/VPythonDocs/math.html), note the portion of the instructions italicized here:

In general, Python modules cannot be imported into VPython programs, because GlowScript VPython functions in a JavaScript environment. However, one can import the Python "random" module into a VPython program, provided by the RapydScript-NG tool that converts Python to JavaScript. One cannot use the form "from random import *" but must use "import random" or "import random as rr" (or other name) or "from random import randint, uniform" (or other list of functions). Here is documentation of the Python random module, however only seed_state, get_random_byte, seed, random, randrange, randint, uniform, choice, shuffle, and sample have been implemented in GlowScript. You do not need to import the random module in order to be able to use the random() function described above.  

I don't understand your question about using "choice" for a list. It sounds like a question about Python, not VPython, though the menu widget does have a "choices" attribute:

choices A list of strings to appear on the menu, such as ['cat', 'dog', 'horse']. Can be changed after creating the menu by specifying a new list of strings to replace the existing list.  

Bruce

Anton Magursky

unread,
Jan 13, 2021, 5:24:56 PM1/13/21
to Glowscript Users
Thanks for quick reply, Bruce!
Why I mean Vpython and not python is because in python I only need 3 lines to perform a random choice:
import random
a = [1,9,3,2]
print(random.choice(a))

and in web version of GlowScripts/Vpython (unfortunately I didn't understand all the difference between them yet) I cannot import random module. But I understood from documentation that some parts of it is already there. Maybe I got it wrong and they (...uniform, choice, shuffle) can be used only on local computer with RapydScript-NG or or else installed. And if so, I probably have to implement my own version of the random functions, something like
L1 = [2, 3, 9, 1]
bucket = 0
threshold = 100/len(L1)
Zone = []

for i in range(len(L1)):
    Zone.append(bucket+threshold)
    bucket += threshold
    
rnd = int(random()*100)
print(rnd)

for i in:...

but isn't it easier to just add this module into code of GlowScript or Vpython? (I'm sorry, I'm not familiar with the complexity of such task yet).
четверг, 14 января 2021 г. в 00:10:45 UTC+2, Bruce Sherwood:

Bruce Sherwood

unread,
Jan 13, 2021, 6:27:31 PM1/13/21
to Glowscript Users
Ah. Thanks. Now I understand.

Apparently few people have used the GlowScript ability to import a (limited) set of methods from the random library, and you are the first to report that in version 3.0 those methods are missing. The program

    import random
    a = [1,9,3,2]
    print(random.choice(a))

DOES work if you start with "GlowScript 2.9 VPython", so something is missing from version 3.0.

Thanks for the bug report.

Bruce

Anton Magursky

unread,
Jan 13, 2021, 6:43:40 PM1/13/21
to Glowscript Users
Wow, that was a bit unexpected workaround.
Looks like somebody was not following TDD principles ;-)
Would you like me (and maybe anybody else in the community) to start creating unit test pull requests for you in the near future?

четверг, 14 января 2021 г. в 01:27:31 UTC+2, Bruce Sherwood:

Bruce Sherwood

unread,
Jan 13, 2021, 9:28:26 PM1/13/21
to Glowscript Users
No effort has gone into creating unit tests because around 99% of the bugs that get reported would not be identified with unit tests, for the simple reason that they almost always are manifest only by a human viewing a 3D scene. Moreover, the failure of "import random" may well have come to pass a year or more ago, and no one reported the problem, so this bug has had essentially zero impact for a very long time.

The main tools for avoiding bugs are two: 1) Before updating glowscript.org the "Example programs" at glowscript.org, at a minimum, are executed to make sure that the animations behave as expected. 2) A large number of diverse users contributes to rapid identification of bugs. Last week it was discovered almost immediately that a major update didn't handle user programs with Chinese names. There are over 100,000 glowscript.org accounts:

    https://www.glowscript.org/#/user/Bruce_Sherwood/folder/My_Programs/program/Users

But sure, if you want to contribute unit tests, fine. Just be aware that effort that goes into that is effort that could be invested in far more important issues.

Bruce

Harlan Gilbert

unread,
Jan 13, 2021, 9:44:36 PM1/13/21
to glowscri...@googlegroups.com
In Glowscript 3.0, the function random() is still available without an import.



--

---
You received this message because you are subscribed to the Google Groups "Glowscript Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glowscript-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/glowscript-users/b9981ab6-905b-416a-ab33-53624a67bbd9n%40googlegroups.com.


--
Harlan Gilbert
Co-Chair and Math, Physics, Computer Science, and Philosophy Teacher
Green Meadow Waldorf High School
Chestnut Ridge, NY 10977

Bruce Sherwood

unread,
Jan 14, 2021, 12:16:34 AM1/14/21
to Glowscript Users
And random() works because lots of people use it. 

Bruce

Anton Magursky

unread,
Jan 19, 2021, 1:53:32 AM1/19/21
to Glowscript Users
Bruce,
Could you please explain why

import random
a = [1,9,3,2]
print(random.choice(a))

works,  but

import random
print(randrange(8,16,3))

doesn't?
I even set "GlowScript 2.9 VPython"
The docs refer to https://docs.python.org/3/library/random.html and according to which this should work.
четверг, 14 января 2021 г. в 07:16:34 UTC+2, Bruce Sherwood:

Anton Magursky

unread,
Jan 19, 2021, 1:55:58 AM1/19/21
to Glowscript Users
Sorry, I mean 

import random
print(random.randrange(8,16,3))

(I actually tried both and neither works)

вторник, 19 января 2021 г. в 08:53:32 UTC+2, Anton Magursky:

Steve Spicklemire

unread,
Jan 19, 2021, 2:48:11 AM1/19/21
to glowscri...@googlegroups.com, Steve Spicklemire
Thanks Anton,

I tried this simple program:

“””
GlowScript 2.9 VPython

import random

x = random.randrange(16)

print(x)
“””

And the GUI reported “Index Error”, but tracking it down it’s throwing an exception in the compiler:

"Error
at IndexError.__init__ (eval at module.exports (eval at load (https://sandbox.glowscript.org/package/RScompiler.2.9.min.js:1:1213288)), <anonymous>:2204:19)
at IndexError.__init__ (eval at module.exports (eval at load (https://sandbox.glowscript.org/package/RScompiler.2.9.min.js:1:1213288)), <anonymous>:2251:66)
at new IndexError (eval at module.exports (eval at load (https://sandbox.glowscript.org/package/RScompiler.2.9.min.js:1:1213288)), <anonymous>:2247:35)
at choice (eval at compileAndRun (https://sandbox.glowscript.org/untrusted/run.js:296:31), <anonymous>:142:19)
at Object.randrange (eval at compileAndRun (https://sandbox.glowscript.org/untrusted/run.js:296:31), <anonymous>:116:16)
at __main__ (eval at compileAndRun (https://sandbox.glowscript.org/untrusted/run.js:296:31), <anonymous>:233:16)
at compileAndRun (https://sandbox.glowscript.org/untrusted/run.js:300:26)
at https://sandbox.glowscript.org/untrusted/run.js:252:21
at f (https://sandbox.glowscript.org/lib/head.js/head.min.js:6:460)
at https://sandbox.glowscript.org/lib/head.js/head.min.js:6:1669

I’ll look into it soon, but (unfortunately) I can’t fix this immediately due to other looming deadlines.

Unfortunately, when I tried:

print(random.choice(range(8, 16, 3)))

(which matches the implementation in the compiler) I get the same “Index Error".

The problem seems to like with assuming range() returns an array. It appears to return an iterator now.

In the mean time you can get the same effect by using the “list” function to convert the return value of range:

print(random.choice(list(range(8, 16, 3))))

I hope that helps!

-steve
> --
>
> ---
> You received this message because you are subscribed to the Google Groups "Glowscript Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to glowscript-use...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/glowscript-users/25a795cb-b560-4457-a8b9-0e09f86a297en%40googlegroups.com.

Anton Magursky

unread,
Jan 19, 2021, 3:52:14 AM1/19/21
to glowscri...@googlegroups.com, Steve Spicklemire
yes, so it looks like the problem is in randrange() itself. because this works:
a = list(range(8, 26, 3))
b = [11,12,13,14]
print(random.choice(a))
print(random.choice(b))
print(random.shuffle(a))
print(random.shuffle(b))

and this doesn't:
print(random.randrange(a))
print(random.randrange(b))

вт, 19 янв. 2021 г. в 09:48, Steve Spicklemire <stevespi...@gmail.com>:

Anton Magursky

unread,
Jan 19, 2021, 3:54:51 AM1/19/21
to glowscri...@googlegroups.com, Steve Spicklemire
sorry, I mean
b = [11,12,13]
:)

вт, 19 янв. 2021 г. в 10:52, Anton Magursky <mag...@gmail.com>:

Harlan Gilbert

unread,
Feb 23, 2021, 4:01:17 PM2/23/21
to Glowscript Users
Hi Bruce,

The randint function still does not work in Glowscript 3.0.  Is there a plan to fix this?

Hoping so, and best wishes,
Harlan

Bruce Sherwood

unread,
Feb 23, 2021, 8:40:11 PM2/23/21
to Glowscript Users
It's on the list, but at the moment I don't even understand why it now fails. There are two workarounds:

1) Use GlowScript 2.9

2) Use this function:

def randint(a,b):
    return int(a+(round( (b-a)*random() )))

Bruce

Harlan Gilbert

unread,
Feb 23, 2021, 9:06:22 PM2/23/21
to glowscri...@googlegroups.com
Thanks.  

--

---
You received this message because you are subscribed to a topic in the Google Groups "Glowscript Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/glowscript-users/zzUuLIsuTOw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to glowscript-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/glowscript-users/c732cb4c-13a0-4817-bcfd-00964a554a5bn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages