Generating random nos without using random module

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 14
Reputation: sanchitgarg is an unknown quantity at this point 
Solved Threads: 0
sanchitgarg sanchitgarg is offline Offline
Newbie Poster
 
0
  #11
29 Days Ago
Originally Posted by ov3rcl0ck View Post
Probably the best inputs is date/time, used memory, random user input, and my favorite mouse movement(truecrypt uses this).

Right now i have used time for my input. Could you please give a bit more info on used memory and mouse movement?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso
 
0
  #12
29 Days Ago
Originally Posted by sanchitgarg View Post
Is there any other method or logic except that formula?
There are of course others out there. But this one is the simplest and produce decent set. You can search for pseudorandom number generator algos.

http://en.wikipedia.org/wiki/List_of_algorithms

>> Probably the best inputs is date/time, used memory, random user input, and my favorite mouse movement(truecrypt uses this).

These all will help you to seed......but to generate sets you need some method!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: sanchitgarg is an unknown quantity at this point 
Solved Threads: 0
sanchitgarg sanchitgarg is offline Offline
Newbie Poster
 
0
  #13
28 Days Ago
Originally Posted by vishesh View Post
There are of course others out there. But this one is the simplest and produce decent set. You can search for pseudorandom number generator algos.

http://en.wikipedia.org/wiki/List_of_algorithms

>> Probably the best inputs is date/time, used memory, random user input, and my favorite mouse movement(truecrypt uses this).

These all will help you to seed......but to generate sets you need some method!
I have searched wikipedia n saw many other algos. Rest are too compicated.

Wats the meaning of seed?

n sets refers to the list of random nos?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,034
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 933
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #14
28 Days Ago
This Python code shows you how to pick a random number from a list ...
  1. # picking random numbers from a list
  2. # Python2 syntax
  3.  
  4. def pick_rand(rand_list, start=-1, incr=1):
  5. """
  6. closure based function that cycles through rand_list one index
  7. at a time for each call, index resets to zero at end of list
  8. """
  9. def inner():
  10. inner.index += incr
  11. if inner.index >= len(rand_list):
  12. inner.index = 0
  13. return rand_list[inner.index]
  14. inner.index = start
  15. return inner
  16.  
  17. # random number list of 100 numbers from 0 to 99
  18. # created by timing user input events
  19. rand_list = [
  20. 6, 40, 3, 6, 93, 81, 84, 56, 43, 31, 34, 21, 9, 96, 84, 87, 6, 9,
  21. 12, 0, 87, 59, 31, 18, 6, 9, 96, 68, 56, 59, 31, 3, 75, 62, 34, 21,
  22. 9, 96, 68, 56, 43, 0, 71, 59, 31, 34, 6, 78, 65, 37, 25, 96, 84, 71,
  23. 59, 46, 34, 6, 9, 81, 53, 40, 28, 84, 56, 43, 65, 84, 87, 59, 62,
  24. 50, 21, 9, 96, 84, 71, 59, 46, 18, 90, 93, 81, 84, 71, 59, 31, 34,
  25. 53, 40, 46, 3, 75, 75, 50, 6, 65, 21, 87, 15
  26. ]
  27.  
  28. # testing only ...
  29. print len(rand_list) # 100
  30. print "average = %d" % (sum(rand_list)//len(rand_list)) # 48
  31.  
  32. # needed!
  33. rand = pick_rand(rand_list)
  34.  
  35. # show 10 random numbers
  36. for k in range(10):
  37. print rand(),
  38.  
  39. print
  40.  
  41. # show another 10 random numbers
  42. for k in range(10):
  43. print rand(),
The list is generated this way ...
  1. # a time based random number generator
  2. # that uses the random time between a user's input events
  3. # to create a list of random numbers
  4. # Python2 syntax
  5.  
  6. import time
  7.  
  8. def random_number(low, high):
  9. """
  10. a time based random number generator
  11. uses the random time between a user's input events
  12. returns an integer between low and high-1
  13. """
  14. return int(low + int(time.time()*1000) % (high - low))
  15.  
  16. low = 0
  17. high = 100
  18. count = 0
  19. rand_list = []
  20. print "Press enter for a random number (q to quit):"
  21. while True:
  22. sel = raw_input()
  23. if sel == 'q':
  24. break
  25. rand = random_number(low, high)
  26. rand_list.append(rand)
  27. # give user feedback
  28. print "%d --> %d" % (count, rand)
  29. count += 1
  30.  
  31. print rand_list
  32.  
  33. """my rand_list 0f 100 random numbers between 0 and 99 -->
  34. [6, 40, 3, 6, 93, 81, 84, 56, 43, 31, 34, 21, 9, 96, 84, 87, 6, 9,
  35. 12, 0, 87, 59, 31, 18, 6, 9, 96, 68, 56, 59, 31, 3, 75, 62, 34, 21,
  36. 9, 96, 68, 56, 43, 0, 71, 59, 31, 34, 6, 78, 65, 37, 25, 96, 84, 71,
  37. 59, 46, 34, 6, 9, 81, 53, 40, 28, 84, 56, 43, 65, 84, 87, 59, 62,
  38. 50, 21, 9, 96, 84, 71, 59, 46, 18, 90, 93, 81, 84, 71, 59, 31, 34,
  39. 53, 40, 46, 3, 75, 75, 50, 6, 65, 21, 87, 15]
  40. """
I made the rand_list relatively short for this example, normally you may want to go for a length of >1000.
Last edited by vegaseat; 28 Days Ago at 1:36 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 108
Reputation: ov3rcl0ck is an unknown quantity at this point 
Solved Threads: 12
ov3rcl0ck ov3rcl0ck is offline Offline
Junior Poster
 
0
  #15
25 Days Ago
Originally Posted by sanchitgarg View Post
Right now i have used time for my input. Could you please give a bit more info on used memory and mouse movement?
Fetching memory usage and mouse input are OS dependent. There isn't a library to fetch memory usage, but you can use the "mem" command on windows to fetch it, and the "ps" or "free" command on UNIX to fetch it, but keep in mind you might need to use a little regex or at least some string functions. For mouse input you can use pyHook for windows, and xlib for UNIX(OS X supports X11, and any Unix distro worth using supports it).

If you need me to I can post some examples of pyHook, xlib, and memory fetching.
NOTE: sudo doesn't apply to real life situations.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: sanchitgarg is an unknown quantity at this point 
Solved Threads: 0
sanchitgarg sanchitgarg is offline Offline
Newbie Poster
 
0
  #16
22 Days Ago
hey thanks everybody for the help.
i wud be using the Linear congruential generator(LCG) only for implementing my code.

thanks again
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,034
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 933
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #17
21 Days Ago
Originally Posted by sanchitgarg View Post
hey thanks everybody for the help.
i wud be using the Linear congruential generator(LCG) only for implementing my code.

thanks again
Well, once you have it down to Python code, let us know.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: sanchitgarg is an unknown quantity at this point 
Solved Threads: 0
sanchitgarg sanchitgarg is offline Offline
Newbie Poster
 
0
  #18
21 Days Ago
Originally Posted by vegaseat View Post
Well, once you have it down to Python code, let us know.

yes sir!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC