0
Offline
Newbie
Karma: 0
Posts: 37
shrz
|
 |
« on: July 13, 2007, 06:03:21 pm » |
Hello there!
I need to create by random really large numbers. Does the random() function whose type is set as "long", return numbers in the range of long? between 0 and 2,147,483,647? when i try to check it by printing the value over serial, it returns always 16807.
any suggestions?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 37
shrz
|
 |
« Reply #1 on: July 13, 2007, 07:22:19 pm » |
mh. im not shure yet. i thought about the serial. What is the maximum value i can transmit over serial? is it the "int" range? if it is so, how can i send values of the size of "long" over the serial? (perhaps i should start another topic for that)
woo
|
|
|
|
« Last Edit: July 14, 2007, 07:02:36 am by woo_oow »
|
Logged
|
|
|
|
|
0
Offline
Faraday Member
Karma: 6
Posts: 2504
|
 |
« Reply #2 on: July 14, 2007, 07:06:59 am » |
what does your random() call look like?
Integer constants that you want to be of type long need an "L" at the end, e.g. i = random(2000000L);
-j
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 37
shrz
|
 |
« Reply #3 on: July 14, 2007, 07:27:06 am » |
at the moment it looks like:
long x = random(1576800000); (actually its the amount of seconds of 50 years)
so it should look like: long x = random(1576800000L);?
thanks w
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Faraday Member
Karma: 6
Posts: 2504
|
 |
« Reply #4 on: July 14, 2007, 12:58:01 pm » |
so it should look like: long x = random(1576800000L); that's correct. -j
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 37
shrz
|
 |
« Reply #5 on: July 14, 2007, 01:08:53 pm » |
Thanks! this very good to know.
w
|
|
|
|
« Last Edit: July 14, 2007, 01:36:03 pm by woo_oow »
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 37
shrz
|
 |
« Reply #6 on: July 14, 2007, 01:35:01 pm » |
There is still a problem: over the serial i get only values until 5 digits. for evaluation i used this code:
long x; void setup(){ Serial.begin(9600); } void loop(){ x=random(1576800000L); Serial.println (x); delay(100); }
it returns values until 32 thousand something. i dont need the serial thing, but i want to be shure if the random numbers are created throughout the entire range. is it just a serial problem?
w
|
|
|
|
|
Logged
|
|
|
|
|
Forum Administrator
Cambridge, MA
Offline
Faraday Member
Karma: 8
Posts: 3532
|
 |
« Reply #7 on: July 15, 2007, 08:14:03 am » |
Whoops, looks like you found a bug in the core. We're calling the wrong AVR function, so you'll only get numbers from 0 to 32,767. You could try calling random() twice and putting the number together, e.g.:
(random(32768) << 15) + random(32768);
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 37
shrz
|
 |
« Reply #8 on: July 15, 2007, 02:38:46 pm » |
Thanks for your answer!
this could be a solution. I'll try it.
best
w
|
|
|
|
|
Logged
|
|
|
|
|
Sweden
Offline
Newbie
Karma: 0
Posts: 27
Arduino really rocks
|
 |
« Reply #9 on: August 10, 2007, 06:39:29 pm » |
Did you solve it?
By the way, Mellis solution doesn't really get up to your needs. We need one more bit:
(random(1) << 30) + (random(32768) << 15) + random(32768);
If anyone is intrested in a deeper explanation, here is my late-night go:
The random() function can random a highest number of 32,767 which would be it returns 15 bits of random data, so in order to get a larger number we need to connect two or more of those 15 bit chunks.
What we are doing here: (random(32768) << 15) is simply that we push randomed bits 15 bits forward on the variable: 00000000 00000000 0xxxxxxx xxxxxxxx gets into: <----15 steps 00xxxxxx xxxxxxxx x0000000 00000000
Then we add with the next chunk of random data: 00xxxxxx xxxxxxxx x0000000 00000000 + 00000000 00000000 0xxxxxxx xxxxxxxx = 00xxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
Oh, i noticed now that your maximum random number doesn't follow any 2^x number... ok, lets see...
We need to find the largest exponentation in the number: 1 576 800 000 - 1 073 741 824 = 503 058 176
Now we do the same with our result: 503 058 176 503 058 176 - 268 435 456 = 234 622 720
234 622 720 - 134 217 728 = 100 404 992
Oh heck, i am too tired, just look up which bits in 1 576 800 000 are 1 and make one random(2^x) for each of them where x is the position of the bit (fir example if first bit (bit numer 0) is true, max random is 2 ^ 0 = 1. If the maximum gets over 32 767 just split together like i explained above.
Goodnight!
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 37
shrz
|
 |
« Reply #10 on: August 13, 2007, 12:44:33 am » |
Thanks man,
you're right, like this it should generate numbers throughout the field i need, thats great. i'll try this soon. (at the moment i'm in shanghai, for visiting a friend - its the ultimate electronic parts paradise, you find everything you ever dreamed of!!!)
w
|
|
|
|
|
Logged
|
|
|
|
|
|
|
 |
« Reply #11 on: August 13, 2007, 01:21:03 am » |
(at the moment i'm in shanghai, for visiting a friend - its the ultimate electronic parts paradise, you find everything you ever dreamed of!!!)
is it better than this? I was in Seoul a month ago... They have whole malls full of IC's.. http://www.flickr.com/photos/electromarketman/sets/72157601136408488/
|
|
|
|
|
Logged
|
|
|
|
|
Sweden
Offline
Newbie
Karma: 0
Posts: 27
Arduino really rocks
|
 |
« Reply #12 on: August 14, 2007, 03:35:48 pm » |
I am so going to Seoul! Just have to persuade my family that they want to go there aswell.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 37
shrz
|
 |
« Reply #13 on: September 10, 2007, 11:09:03 am » |
Its really great to be at places like this! Daniel, the pictures of Seoul are looking like the Malls in Shanghai. Fantastic.
@ Mellis: in Arduino 0009 is there still a Bug in Random()?
w
|
|
|
|
|
Logged
|
|
|
|
|
Forum Administrator
Cambridge, MA
Offline
Faraday Member
Karma: 8
Posts: 3532
|
 |
« Reply #14 on: September 10, 2007, 12:06:53 pm » |
Yes, sorry, I didn't have a chance to fix it. Don't worry it's still on the list, hopefully I'll get to it in 0010.
|
|
|
|
|
Logged
|
|
|
|
|
|