Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5966133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:45:03+00:00 2026-05-22T19:45:03+00:00

from random import * N = 100 gamma = 0.7 connect = zeros((N,N)) for

  • 0
from random import *
N = 100
gamma = 0.7
connect = zeros((N,N))

for i in range(N):
    for j in range(i+1):
        if random() < gamma:
            connect[i,j] = 1
            connect[j,i] = 1
        else:
            connect[i,j] = 0
            connect[j,i] = 0

What I try to do is to create a symmetrical matrix, filled with zeros and ones (ones with a probability of 0.7).
Here is the double for loop, very inefficient…I shall make something with numpy, which I believe could speed up thing a great deal?
Does anyone know how to proceed?
Thank you very much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:45:04+00:00Added an answer on May 22, 2026 at 7:45 pm

    You could use the numpy random module to generate random vectors, and use those vectors to seed the matrix. For example:

    import numpy as np
    
    N = 100
    gamma = 0.7
    connect = np.zeros((N,N),dtype=np.int32)
    
    for i in range(0,N):
            dval = np.diag((np.random.random_sample(size=(N-i))<gamma).astype(np.int32),i)
            connect += dval
            if (i>0):
                    connect += dval.T
    

    does this diagonally using numpy.diag, but you could do it row-wise to assemble the upper or lower triangular portion, then use addition to form the symmetrical matrix. I don’t have a feeling for which might be faster.


    EDIT:
    In fact this row wise version is about 5 times faster than the diagonal version, which I guess shouldn’t be all that surprising given the memory access patterns it uses compared to diagonal assembly.

    N = 100
    gamma = 0.7
    connect = np.zeros((N,N),dtype=np.int32)
    
    for i in range(0,N):
        rval = (np.random.random_sample(size=(N-i))<gamma).astype(np.int32)
        connect[i,i:] = rval
    
    connect += np.triu(connect,1).T
    

    EDIT 2

    This is even simpler and about 4 times faster than the row-wise version above. Here a triangular matrix is formed directly from a full matrix of weights, then added to its transpose to produce the symmetric matrix:

    N = 100
    gamma = 0.7
    a=np.triu((np.random.random_sample(size=(N,N))<gamma).astype(np.int32))
    connect = a + np.triu(a,1).T
    

    On the Linux system I tested it on, version 1 takes about 6.5 milliseconds, version 2 takes about 1.5 milliseconds, version 3 takes about 450 microseconds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

from random import randrange data = [(randrange(8), randrange(8)) for x in range(8)] And we
I am creating a random ID using the below code: from random import *
How to import json file in action script and then get random element from
from sys import exit from random import randint class Map(object): def death(): print quips[randint
I have some question about functions which have default parameters. import sys from random
I have a program that uses the mt19937 random number generator from boost::random. I
How do I retrieve an item at random from the following list? foo =
How do I pick a random element from a set? I'm particularly interested in
I want to put random output from my result set (about 1.5 mil rows)
I am getting random row from my table using the following query: SELECT value_id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.