Hot Topic (More than 10 Replies) Random Numbers (Read 1059 times)
dhopkins
Member
*
Offline



Posts: 47
Joined: Aug 20th, 2007
Random Numbers
Dec 31st, 2007 at 5:54pm
Print Post Print Post  
I have a subroutine in the Global code section that picks a random number and then uses that as an array index. But the number only seems to be random once. Each time the subroutine is called, the same number is getting used. Here's a snippet.

subroutine show_random_message()
     var array_messages as ARRAY[3] of String
     var text_status as String

     var random_number as Double = @int(random(2)+1)

     array_messages[1] = "Hello!";
     array_messages[2] = "Hi!!";
     array_messages[3] = "Hola!";
     text_status = random_number + array_messages[random_number]
     Label(Status,text_status)


end subroutine

D
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Random Numbers
Reply #1 - Dec 31st, 2007 at 6:10pm
Print Post Print Post  
dhopkins,

I tried your code and am getting different numbers. Keep in mind that Random returns a double. You are then converting the double to an int. With such a small sampling as Random(2), you only have a few ints possible.

Try using WriteLn to print out the double value before converting it to Int to see if you are actually getting the same number over and over or if you just keep getting doubles that convert to the same int value.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
dhopkins
Member
*
Offline



Posts: 47
Joined: Aug 20th, 2007
Re: Random Numbers
Reply #2 - Dec 31st, 2007 at 6:23pm
Print Post Print Post  
Hi I dropped the number of random numbers in the snippet for space purposes. In the actual program there are 10 items in the array.

I added the writeln and the numbers are showing up as the same.

I'm calling the subroutine from an OnElementEntry call. Would that affect anything?

D
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Random Numbers
Reply #3 - Dec 31st, 2007 at 6:51pm
Print Post Print Post  
What did you WriteLn out? Can I see the code?
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
dhopkins
Member
*
Offline



Posts: 47
Joined: Aug 20th, 2007
Re: Random Numbers
Reply #4 - Dec 31st, 2007 at 7:01pm
Print Post Print Post  
Sure, here's the whole thing. The numbers after the decimal are random, but the integer part is always the same after the first time the subroutine is called.
So I'll get 2.088, 2.112, 2.778 etc...and when I restart the app, I'll get 1.988, 1.872,1.324, etc.

subroutine show_random_message()
     var array_messages as ARRAY[10] of String
     var text_status as String

     var random_number as Double
               array_messages[1] = "Hi!";
     array_messages[2] = "Hello!";
     array_messages[3] = "Hola!";
     array_messages[4] = "Bon Jour!";
     array_messages[5] = "Guten Tag?";
     array_messages[6] = "Beinvenue!";
     array_messages[7] = "Hey!";
     array_messages[8] = "Howdy!";
     array_messages[9] = "011011011!";
     array_messages[10] = "Sup!";
     
     random_number = random(9)+1
     writeln(random_number)
     random_number = @int(random_number)
     text_status = array_messages[random_number]
     Label(Status,text_status)
     

end subroutine
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Random Numbers
Reply #5 - Dec 31st, 2007 at 7:21pm
Print Post Print Post  
I don't get the behavior you are seeing...which makes sense...because it's randomized.  Smiley

You may be able to reboot or move to a different computer and change the behavior. At any rate, you are not actually getting the same numbers. You are getting a range of quasi-random numbers that are not distributed as far apart as you would like. It looks like a random selection isn't what you want anyway. You seem to want something that looks random but is actually is not. If you want Random to do a better job of pretending to do this, try increasing the range it has to work with by doing something like this :

Code
Select All
     random_number = random(999)
     random_number = @int(random_number / 100)
     if random_number = 0 random_number = 10
     writeln(random_number) 

  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Re: Random Numbers
Reply #6 - Dec 31st, 2007 at 7:22pm
Print Post Print Post  
Try

Code
Select All
Subroutine show_random_message()
Var array_messages as ARRAY[10] of String
Var text_status as String
Var random_number as Double
Var number_options as Int

	array_messages[1] = "Hi!";
	array_messages[2] = "Hello!";
	array_messages[3] = "Hola!";
	array_messages[4] = "Bon Jour!";
	array_messages[5] = "Guten Tag?";
	array_messages[6] = "Beinvenue!";
	array_messages[7] = "Hey!";
	array_messages[8] = "Howdy!";
	array_messages[9] = "011011011!";
	array_messages[10] = "Sup!";

	number_options = 10
	number_options = number_options + 1
	random_number = 0
	While ((random_number < 1) Or (random_number = number_options))
	{
		random_number = random(number_options)
	}
	writeln(random_number)
	random_number = @int(random_number)
	text_status = array_messages[random_number]
	Label(Status,text_status)

End Subroutine 



-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
dhopkins
Member
*
Offline



Posts: 47
Joined: Aug 20th, 2007
Re: Random Numbers
Reply #7 - Jan 2nd, 2008 at 3:20pm
Print Post Print Post  
Hi,
Both suggestions still gave me basically the same results as before. So instead of beating my head against the wall, I decided to go old school. Since the decimal section was random, I just used that part of it.

random_number = random(999)+1
decoy = @int(random_number)
random_number = random_number - decoy
random_number = random_number * 10
random_number = @int(random_number)

not pretty, but it works. Random integer between 1 and 10

D
  
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Random Numbers
Reply #8 - Jan 2nd, 2008 at 3:45pm
Print Post Print Post  
I'm interested in why your computer is producing such unusual results from Random(). If you have a moment, could you run the following code and post the result here. You can paste this code into a single record mass update to run it:
Code
Select All
var bb as double
var total as double
var loop as int

  for loop = 1 to 1000
    bb = Random(10)
    total = total + bb
  next
  writeln(total / 1000)
 

  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1350
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Random Numbers
Reply #9 - Jan 2nd, 2008 at 4:05pm
Print Post Print Post  
Yeah, I was also wondering that, because this simple code produces a pretty good random result.

Code
Select All
var i as int

For i = 1 to 20
	Result = Result + (@Int(Random(10))+1) + @NL()
Next 


Produced this:
10
3
4
7
2
10
1
8
4
7
10
5
1
10
7
9
6
8
2
4
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
dhopkins
Member
*
Offline



Posts: 47
Joined: Aug 20th, 2007
Re: Random Numbers
Reply #10 - Jan 2nd, 2008 at 5:11pm
Print Post Print Post  
Quote:
I'm interested in why your computer is producing such unusual results from Random(). If you have a moment, could you run the following code and post the result here. You can paste this code into a single record mass update to run it:


That code resulted in the following after a few runs
5.07084841
4.94540696
5.00005127
5.08477981
5.04436476
4.99403088

Weird huh?

D

  
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Random Numbers
Reply #11 - Jan 2nd, 2008 at 5:21pm
Print Post Print Post  
dhopkins wrote on Jan 2nd, 2008 at 5:11pm:
Quote:
I'm interested in why your computer is producing such unusual results from Random(). If you have a moment, could you run the following code and post the result here. You can paste this code into a single record mass update to run it:


That code resulted in the following after a few runs
5.07084841
4.94540696
5.00005127
5.08477981
5.04436476
4.99403088

Weird huh?

D



Not weird at all. That is the expected result and shows that you are getting a perfectly even distribution of random numbers between 1 and 10. The result in this case is the average of the 1000 random numbers. It should always land very near 5.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged