Random Nonzero

tphanyamada shared this question 2 years ago
Answered

Dear all,

I would to get a random nonzero number between -1 and 1.


Is there a command for random nonzero number on GeoGebra?

Thank you so much for your help.

Comments (10)

photo
1

2 RandomBetween(0, 1)-1

photo
1

Thank you! But I got 0. I'd like to have a nonzero number.

photo
1

have you write randombetween( ) or random( )?

2 RandomBetween(0, 1)-1 =0 is not posible

photo
1

Does this code me 2* random number between (0,1) - 1?

So how do I choose a non-zero random number between (-n, n)?

photo
1

(2 RandomBetween(0, 1)-1) RandomBetween(1, n)

photo
photo
1

Probably there's a shorter way but this works: RandomElement(RandomUniform(-1, 1, 4)\{0})

RandomUniform(Min,Max,Number) creates a list of random real numbers between min and max. So if you exclude 0 uot is these list (that's why I worked with a list) and take a random element out of it you get a random real number between -1 and 1 while 0 is excluded.

Aa alternative is a thing as RandomElement({RandomUniform(-1, -0.0001),RandomUniform(0.0001, 1)})

This creates a random real negative number from -1, and a real positive number til 1 and picks one of the two, while 0 is excluded. A nice exercise to think of a lot of alternatives

chris

chris

photo
1

Actually, I'd like to choose a non-zero random integer between (-n, n). How would I do it?

photo
2

In that case you can do with a command as RandomElement(Sequence(-n, n)\{0}) if indeed this is actually what you wanted to ask

chris

photo
1

Maybe neater?

RandomBetween(1,n) * RandomElement({-1,1})

photo
1

To all,

Thank you so much for your code.

They all work well.

Many thanks,

photo
© 2022 International GeoGebra Institute