# Random numbers with counting columns

vestLA shared this question 2 years ago
Answered

I am pretty new to using GeoGebra scripts, so I hope someone can help me get started with the following task:

I need to have generated say n = 500 random real numbers in the interval from 0 to 1. Two integers, a and b, are supposed to act as counting variables. Initially they are set to 0. One by one each element in the list with random numbers should be evaluated if is below 0.4 or above. If it is below, a should be updated by 1, if not, b should be updated by 1. Everytime an an element in the list has been evaluated, two rectangles A and B (columns) with heights representing the current value of a and b respectively, should be updated as well. In the end the height of column A will have an height equal to the number of elements in the list which are less than or equal to 0.4, and column B will hold the number of elements in the list which are greater than 0.4.

I hope someone can help.

Erik V.

## Comments (9)

2

How's that to start?

```l1=Sequence(random(), i, 1, 500)
a=Length(KeepIf(x < 0.4, l1))
b=Length(KeepIf(x >= 0.4, l1))```

1

Thanks for the input! The command Keeplf is nice, but because of educational reasons (pedagogics), I want the columns not to show up instantly, but to 'grow up slowly' to the final height, maybe in about 3-5 seconds, if possible. Just like in an opinion poll ...

Thanks, Erik.

2

You could build a slider, n, that runs from 1 to 500, which can then be animated.

If you create l2=first(l1, n), then refer a and b to l2 instead of l1, then a and b will "grow" as n increases in the animation.

, this will

2

or without registering the random() values

try to move the blue dot

Files: foro.ggb
1

Hi again

It took me a while to absorb the information, but after having looked around on the GeoGebra homepage and testing things on my own, I finally made it work. I am totally new on scripting in GeoGebra and I was running my head against the brickwall more than once. Unfortunately some of the GeoGebra tutorials are old and have not been updated. For example I looked after an OK button in the Properties panel, as the tutorial said, but it was not there. I assume something have been updated, so one doesn't need that anymore. Also for a long time, I didn't know why the On Click scripting didn't work for me. I had to watch a YouTube video to find out, that pressing a button only works if one closes the Properties panel! After I closed it, it worked! I guess quite a few tutorials needs an update on the GeoGebra homepage - especially on the scripting side, it needs some documentation. Anyway, I am very pleased with what this program can do. It is for free and the graphics just look "delicious". The creators deserve a lot of credit!!

In my attached GeoGebra file, I am using a combination of Borcherds and pirsquared ideas. I have a few questions:

a) I will be delighted if you could please tell me if I am doing it in the most appropriate way or it can be simplified? Especially in the On Click scripting associated with the button "Sample".

b) I am using a polygon Tool for the column. Assume I had to do with several columns for many parties in an Opinion Poll. Is there a more appropriate way to create the columns in this respect?

c) Is there a way to control the Viewing area? I know scrolling and using Ctrl keys will zoom in or out, but I would like to set a "Window" for the most appropriate viewing ...

Thanks a lot for the help!

Erik

2

Try changing this

```list1=Sequence(random(), i, 1, N)
H=Length(KeepIf(x < 0.4, list2))
n=1
StartAnimation(n)```

to

```UpdateConstruction()
SetValue(n,1)
StartAnimation(n)```
It's best to avoid "=" in scripts.

Have a look at https://wiki.geogebra.org/e... for plotting several rectangles :)

1

OK, thanks a lot. Looks more streamlined indeed. And yes, probably BarCharts are what I need here ...

Erik

1

I have been using used your good advice and developed my GeoGebra file quite a lot (attached). First I realized that using a BarChart is the most convenient way by far, especially when using more columns as I am doing in my new version. I have also started using a spreadsheet as a convenient way of controlling input as well as output. The user is assumed to write the letters for a number of different political parties in the A column. In the B column the user is supposed to write the population frequencies (assumed known).

When pressing the Sample button, I want GeoGebra to create a simple random sample of size N (user input in a TextField). Given the list of population frequencies I have created a "Population Frequencies Cumulative" list PFC and adding 0 to it. Using that list, I can easily decide which party in the list will get the 'vote' associated with a given random number between 0 and 1. I am still using your brilliant idea of animating the voting process.

I still have a couple of questions still though:

1. Is there a way that GeoGebra can by itself read the number of parties written in the spreadsheet. Or do I need to let the user input the number in a Text field as it is now?

2. My variable SampleProcentages is containing the final sample procentages and I want them to be displayed in the pink column C in the spreadsheet. As it is now, I have manually written them as SampleProcentages(1), SampleProcentages(2), etc. in column C. I want however GeoGebra to do it automatically: I don't know how many parties are present!

3. Is the animation process efficient? Yesterday I did some work on the file, and I experienced several instances of GeoGebra getting stuck before n got all the way to N. I had to stop the Java-process from the Joblist and start all over. I did some changes today, and I am not sure what made it work better. It seems to work now, even if I press the Sample button several times (after each animation have been completed). Does the command UpdateConstruction() do some unnecessary updating? I don't have an overview at all over this issue at all ...

Thanks a lot for the current help I have received here in this forum! It has been great, and is helping me overcoming obstacles when I get stuck. I have found out quite a lot the recent days.

Erik

1

I did succeed in solving the second question above. I also streamlined the document by rearranging the order of commands and adding comments (captions) to objects. That way I have got a much better idea what is going on. I am pretty satisfied now. To make it complete, though, I would like to have the Party letters placed below the columns as labels. The BarChart command doesn't seem to allow this?

Also I wonder if it is possible to somehow extract the number of rows being filled out by the user in the spreadsheet. If so the user wouldn't have to enter the number of parties in a text field.

Erik

Leave a Comment
• GeoGebra
• Help
• Partners
• Contact us
• Feedback & Questions
• This email address is being protected from spambots. You need JavaScript enabled to view it.
• +43 677 6137 2693
© 2023 International GeoGebra Institute